在此示例中,我们向您展示如何使用JDOM解析器在XML文件中创建文档,元素和属性。
1. XML文件
在本示例的最后,将创建以下XML文件。
文件:file.xml
<?xml version="1.0" encoding="UTF-8"?>
<company>
<staff id="1">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>199999</salary>
</staff>
<staff id="2">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>188888</salary>
</staff>
</company>
3. JDOM示例
创建以上XML文件的JDOM示例。
import java.io.FileWriter;
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 WriteXMLFile {
public static void main(String[] args) {
try {
Element company = new Element("company");
Document doc = new Document(company);
doc.setRootElement(company);
Element staff = new Element("staff");
staff.setAttribute(new Attribute("id", "1"));
staff.addContent(new Element("firstname").setText("yong"));
staff.addContent(new Element("lastname").setText("mook kim"));
staff.addContent(new Element("nickname").setText("mkyong"));
staff.addContent(new Element("salary").setText("199999"));
doc.getRootElement().addContent(staff);
Element staff2 = new Element("staff");
staff2.setAttribute(new Attribute("id", "2"));
staff2.addContent(new Element("firstname").setText("low"));
staff2.addContent(new Element("lastname").setText("yin fong"));
staff2.addContent(new Element("nickname").setText("fong fong"));
staff2.addContent(new Element("salary").setText("188888"));
doc.getRootElement().addContent(staff2);
// new XMLOutputter().output(doc, System.out);
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(doc, new FileWriter("c:\\file.xml"));
System.out.println("File Saved!");
} catch (IOException io) {
System.out.println(io.getMessage());
}
}
}
翻译自: https://mkyong.com/java/how-to-create-xml-file-in-java-jdom-parser/