使用XmlUtil.createXml() 创建Document,使用XmlUtil.toStr(doc) 解析doc内容,使用XmlUtil.toFile(doc,path)保存文件
public static void main(String[] args) throws ParserConfigurationException, TransformerException {
Document doc = XmlUtil.createXml();
doc.setXmlStandalone(false);// 不显示standalone="no"
Element rootElement = doc.createElement("person");
doc.appendChild(rootElement);
Element nameElement = doc.createElement("name");
nameElement.appendChild(doc.createTextNode("John Doe"));
rootElement.appendChild(nameElement);
Element ageElement = doc.createElement("age");
ageElement.appendChild(doc.createTextNode("30"));
rootElement.appendChild(ageElement);
//获取项目根目录
String property = System.getProperty("user.dir");
String filePath=property+"/"+"1.xml";//生成xml路径
String contextStr = XmlUtil.toStr(doc);
System.out.println("path = " + filePath);
System.out.println("str = " + contextStr);
//保存xml文件
XmlUtil.toFile(doc,filePath);
}
















