开发者

How to append data to an XML document instead of overwrting existing data using dom4j?

How to open an existing XML document in write mode and append new nodes to the root element? I'm using dom4j library but confused the way to do it. I want to incrementally add the da开发者_StackOverflow社区ta to XML file but lost myself in choas. A brief program structure might help me.


It is a multiple step process :

  1. Read the XML from the file or whatever using dom4j, using SAXReader or similar classes. This will produce a dom4j Document object, which means that the XML file has been parsed, eventually validated, and loaded in RAM.
  2. Now the document is loaded in RAM and repesented using instances of Document, Element, etc.. Manipulate the Document object, using the methods it offers. For example, .getRootElement will give you the root element, which is an instance of Element. On it you can call .addChild, .removeChild etc...
  3. Save your XML back to the file or stream it wherever you want, using XMLWriter or similar classes.

Dom4j offers very good documentation and introduction material at http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html . You can have a look at javadocs there to discover all methods that you can use to manipulate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜