有没有一种方法可以告诉xml转换器按字母顺序排列给定XML标记的所有属性?因此,可以说...使用javax.xml.transform.Transformer为漂亮打印排序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](http://xerces.apache.org/xerces2-j/javadocs/api/org/w3c/dom/DOMConfiguration.html)中支持。这可能是一个开始。 – 2012-02-08 21:08:54