import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DomTest {
public static void main(String[] args) {
Document document = DocumentHelper.createDocument();
org.dom4j.Element element = document.addElement("people");
org.dom4j.Element e1 = element.addElement("student");
org.dom4j.Element e2 = e1.addAttribute("id", "101");
e2.addElement("name").addText("houyong");
e2.addElement("age").addText("22");
e2.addElement("sex").addText("Male");
try {
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File("src/my.xml")), new OutputFormat().createPrettyPrint());
try {
xmlWriter.write(document);
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
DOM4j
最新推荐文章于 2022-11-04 11:32:58 发布