<span style="font-size:18px;">package com.demo;
import java.io.File;
import java.io.StringWriter;
import java.security.KeyStore.Builder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
public class createxml {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
//创建根节点
Element root = document.createElement("languages");
root.setAttribute("cat", "it");
Element lan1 = document.createElement("lan");
lan1.setAttribute("id", "1");
Element name1 = document.createElement("name");
name1.setTextContent("java");
Element ide1 = document.createElement("ide");
ide1.setTextContent("eclipse");
//以lan1为父节点,加入name1和ide1子节点
lan1.appendChild(name1);
lan1.appendChild(ide1);
Element lan2 = document.createElement("lan");
lan2.setAttribute("id", "2");
Element name2 = document.createElement("name");
name2.setTextContent("Swift");
Element ide2 = document.createElement("ide");
ide2.setTextContent("Xcode");
lan2.appendChild(name2);
lan2.appendChild(ide2);
Element lan3 = document.createElement("lan");
lan3.setAttribute("id", "3");
Element name3 = document.createElement("name");
name1.setTextContent("C#");
Element ide3 = document.createElement("ide");
ide3.setTextContent("Visual Studio");
lan3.appendChild(name3);
lan3.appendChild(ide3);
//以root为根节点加入三个lan子节点,再将root加入document.
root.appendChild(lan1);
root.appendChild(lan2);
root.appendChild(lan3);
document.appendChild(root);
//-----------------------------------------------
//以上为创建xml文件内容,下面需要将该内容生成xml文件,或者以其他方式保存
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(writer));
System.out.println(writer.toString());
//将创建的xml保存至项目路径下,运行后刷新项目即可看到
transformer.transform(new DOMSource(document), new StreamResult(new File("languages.xml")));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
</span>
控制台效果如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><languages cat="it"><lan id="1"><name>C#</name><ide>eclipse</ide></lan><lan id="2"><name>Swift</name><ide>Xcode</ide></lan><lan id="3"><name/><ide>Visual Studio</ide></lan></languages>
提示:当然生成的xml文件是没有格式化的,你也可以打开生成的languages.xml文件使用快捷点Ctrl + i格式化