当前位置: 代码迷 >> Java相关 >> 用java怎样写xml文件解决方案
  详细解决方案

用java怎样写xml文件解决方案

热度:5589   发布时间:2013-02-25 21:48:52.0
用java怎样写xml文件
如题,怎样使用java编写xml文件。

------解决方案--------------------------------------------------------
可以选择的方法/工具有:dom4j,jdom,StAX,JAXB,xstream,xmlpull, 手工拼接等等。
------解决方案--------------------------------------------------------
http://dom4j.sourceforge.net/dom4j-1.6.1/guide
Creating a new XML document

Often in dom4j you will need to create a new document from scratch. Here's an example of doing that.

Java code
import org.dom4j.Document;import org.dom4j.DocumentHelper;import org.dom4j.Element;public class Foo {    public Document createDocument() {        Document document = DocumentHelper.createDocument();        Element root = document.addElement( "root" );        Element author1 = root.addElement( "author" )            .addAttribute( "name", "James" )            .addAttribute( "location", "UK" )            .addText( "James Strachan" );                Element author2 = root.addElement( "author" )            .addAttribute( "name", "Bob" )            .addAttribute( "location", "US" )            .addText( "Bob McWhirter" );        return document;    }}
  相关解决方案