读/写 XML 文件保留原始属性顺序 [英] Read/write XML file preserving original attribute order
问题描述
我正在使用 javax.xml.parsers.DocumentBuilder
来解析现有的 XML 文件并获取 org.w3c.dom.Document
的实例.
I am using javax.xml.parsers.DocumentBuilder
to parse an existing XML file and get an instance of org.w3c.dom.Document
.
在对这个实例进行操作后,我需要将它写回文件.因此,如何使用 XMLStreamWriter
将 org.w3c.dom.Document
对象写入 XML 文件?
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:我问这个,因为我认为我需要使用 XMLStreamWriter
来编写 XML 文件而不会丢失属性顺序.属性顺序很重要,因为在版本树中,由于属性顺序发生变化,我不想看到 XML 文件发生变化.
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.
推荐答案
请注意开始标签中属性规范的顺序或空元素标签不重要.
Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.
因此,XMLStreamWriter 没有提供限制属性排序的方法.
一般来说,XML 建议都会认为属性排序无关紧要,但请参阅 XML 规范化建议 或 规范 XML 建议,如果您的应用程序需要属性排序.
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.
这篇关于读/写 XML 文件保留原始属性顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!