Read/write XML file preserving original attribute order

☆樱花仙子☆ 提交于 2019-12-24 11:35:19

问题


I am using javax.xml.parsers.DocumentBuilder to parse an existing XML file and get an instance of org.w3c.dom.Document.

After manipulations on this instance, i need to write it back to file. Therefore how can I write org.w3c.dom.Document object to XML file using XMLStreamWriter?

PS: I am asking this, because I think I need to use XMLStreamWriter in order to write the XML file without losing attribute orders. Attribute orders are important because in the version tree, because of attribute order changes, i don't want to see XML file changed.


回答1:


Attribute order is insignificant per the XML Recommendation:

Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

Therefore, XMLStreamWriter provides no way to constrain attribute ordering.

In general, the XML recommendations will all consider attribute ordering to be insignificant, but see the section on attribute processing in the XML Normalization Recommendation or the Canonical XML Recommendation if your application has a need for attribute ordering.



来源:https://stackoverflow.com/questions/31570962/read-write-xml-file-preserving-original-attribute-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!