package com.xm.xml.jdom;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import org.jdom2.Attribute;
import org.jdom2.CDATA;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Text;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class CreateXml {
public static void main(String args[]) throws IOException{
Element root=new Element("books");
Element book1=new Element("book");
Element book2=new Element("book");
Element book3=new Element("book");
Element book4=new Element("book");
Text text=new Text("天人");
book1.addContent(text);
CDATA cd=new CDATA("<b>ds</b>");
book2.addContent(cd);
Attribute author=new Attribute("author","create");
root.setAttribute(author);
book1.setAttribute("name", "book1");
book2.setAttribute("name", "book2");
book3.setAttribute("name", "book3");
book4.setAttribute("name", "book4");
Document doc=new Document();
doc.setRootElement(root);
root.addContent(book1);
root.addContent(book2);
root.addContent(book4);
//添加到指定的顺序
root.addContent(2,book3);
XMLOutputter writer=new XMLOutputter();
writer.setFormat(Format.getPrettyFormat().setEncoding("utf-8"));
// writer.output(doc, System.out);
writer.output(doc, new OutputStreamWriter(new FileOutputStream("src/com/xm/xml/create.xml"),"utf-8"));
writer.clone();
}
}
JDom 1.创建xml
最新推荐文章于 2021-03-13 12:28:14 发布