【发布时间】:2012-03-01 06:24:37
【问题描述】:
有没有办法让 xml 转换器按字母顺序对给定 XML 的标签的所有属性进行排序?所以让我们说...
<MyTag paramter1="lol" andTheOtherThing="potato"/>
会变成
<MyTag andTheOtherThing="potato" paramter1="lol"/>
我从我找到的here 和here 的示例中看到了如何格式化它,但是对标签属性进行排序将是我遇到的最后一个问题。
我希望有类似的东西:
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.SORTATT, "yes"); // <-- no such thing
这似乎是他们所说的: http://docs.oracle.com/javase/1.4.2/docs/api/javax/xml/transform/OutputKeys.html
【问题讨论】:
-
如果你能说服转换器以某种方式使用“规范形式”,属性应该按字母顺序排列。 Xerces 在其DomConfiguration 中对此提供了支持。这可能是一个开始。
标签: java xml pretty-print