Java使用jdom生成xml
package com.just.xml;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.input.*;
import org.jdom.output.*;
public class BuildXML {
public void BuildXMLDoc() throws IOException, JDOMException {
Element eeeRoot, rows, flag, content;
Document Doc;
eeeRoot = new Element("employees_information");
Doc = new Document(eeeRoot);
eeeRoot = Doc.getRootElement();
rows = new Element("rows");
flag = new Element("name");
content = flag.setText("C.Y. Shen");
//content = flag.addAttribute("emp_id", "001");
flag = rows.addContent(content);
flag = new Element("age");
content = flag.setText("43");
flag = rows.addContent(content);
flag = new Element("sex");
content = flag.setText("Male");
flag = rows.addContent(content);
rows = eeeRoot.addContent(rows);
// XMLOut.setEncoding("gb2312");
XMLOutputter XMLOut = new XMLOutputter();
//XMLOut.output(Doc, new FileOutputStream("c://test1.xml"));
System.out.println(XMLOut.outputString(Doc));
}
public static void main(String[] args) {
try {
BuildXML s1 = new BuildXML();
System.out.println("Now we build an XML document .....");
s1.BuildXMLDoc();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}