jdom生成xml文件
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- import org.jdom.Attribute;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
-
- public class generateXML {
-
- public static void main(String[] args) throws IOException {
-
- Document doc = new Document();
-
- Element root = new Element("Root");
- doc.setRootElement(root);
-
- Element element = new Element("elementA");
- root.addContent(element);
-
- Attribute att = new Attribute("attA", "中文");
- element.setAttribute(att);
-
-
- element.addContent(new Element("childA").setText("100"));
- element.addContent(new Element("childB").setText("200"));
-
-
-
-
- File file = new File("result.xml");
- XMLOutputter outp = new XMLOutputter();
- Format format = Format.getPrettyFormat();
- format.setEncoding("UTF-8");
- outp.setFormat(format);
- outp.output(doc, new FileOutputStream(file));
- System.out.println("out put file done!");
- }
- }
原文地址:https://www.cnblogs.com/duanxz/p/2615242.html