用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.
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; }}