日期:2014-05-20  浏览次数:20841 次

用java怎样写xml文件
如题,怎样使用java编写xml文件。

------解决方案--------------------
可以选择的方法/工具有:dom4j,jdom,StAX,JAXB,xstream,xmlpull, 手工拼接等等。
------解决方案--------------------
http://dom4j.sourceforge.net/dom4j-1.6.1/guide.html
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;
    }
}