Modify XML node but keep the XML file format intact

后端 未结 3 466
抹茶落季
抹茶落季 2020-12-17 00:57

How may I modify a XML file without any change like attributes ordering, tag expansion and encoding? (My preference is DOM API)

相关标签:
3条回答
  • 2020-12-17 01:23

    One nice option would be using decentxml. I've successfully used it before for programmatically changing a few attributes of a hand-writen XML config file without losing the formatting. It's not DOM, though.

    0 讨论(0)
  • 2020-12-17 01:29

    You could try VTD-XML.

    Since this library builds an index while keeping the file content as-is, its manipulation API will allow to "patch" your file while keeping the rest intact.

    Using the VTD-XML API, you will be able to navigate your XML like a DOM tree (even using XPath) and do some modifications (insert elements, insert attributes, etc.)

    0 讨论(0)
  • 2020-12-17 01:38

    This is not possible with DOM, since DOM does not know the attributes' order.

    You could use a low-level API such as StAX, javax.xml.stream, but StAX is not exactly comfortable to use.

    0 讨论(0)
提交回复
热议问题