package example01;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
public class doit {
public static void main(String[] args) {
Document doc = createDocument();
Element root = doc.createElement("students");
doc.appendChild(root);
addStudentInfo(doc,root,"王宏","20100101","96","88","90");
addStudentInfo(doc,root,"李娜","20100102","75","56","70");
addStudentInfo(doc,root,"孙武","20100103","77","70","20");
outputXMLFile(doc,"student.xml");
}
/* 通过DOM解析器创建一个空的Document对象 */
public static Document createDocument() {
// 通过newInstance方法创建DocumentBuilderFactory对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = null;
try {
// 创建解析器对象
DocumentBuilder db = dbf.newDocumentBuilder();
// 创建一个空的Document对象
doc = db.newDocument();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return doc;
}
public static void addStudentInfo(Document doc,Element root,String studentName,String studentId,String javaContent,String OracleContent,String umlContent){
Element student = doc.createElement("student");
student.setAttribute("id",studentId);
root.appendChild(student);
Element name = doc.createElement("name");
name.setTextContent(studentName);
student.appendChild(name);
Element java = doc.createElement("java");
java.setTextContent(javaContent);
student.appendChild(java);
Element oracle = doc.createElement("oracle");
oracle.setTextContent(OracleContent);
student.appendChild(oracle);
Element uml = doc.createElement("uml");
uml.setTextContent(umlContent);
student.appendChild(uml);
}
/* 将内存中的DOM树输出为一个xml文档 */
public static void outputXMLFile(Document doc,String filename) {
try {
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
//设置输出XML文件的换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
//设置输出XML文件的缩进
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
//把DOM对象转换为XML对象
DOMSource source = new DOMSource(doc);
//创建一个输出XML文件对象
//StreamResult result = new StreamResult(new File(filename));
StreamResult result = new StreamResult(new PrintStream(new FileOutputStream(filename),true, StandardCharsets.UTF_8));
//把XML源代码输出为XML文件
tf.transform(source, result);
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
}
关于编码问题
保存的XML是UTF-8格式
整个java项目也是UTF-8格式
我在写的过程中遇到了保存出来的XML文件有中文乱码的问题。
事情是这样的,老师发的Java文件是Eclipse写的,我用的Idea打开,然后打开中文乱码
于是我就把Java文件项目编码调成了GBK,结果写出来的文件就中文乱码了。
所以我在输出文件的函数那,把StreamResult result = new StreamResult(new File(filename));
改成了StreamResult result = new StreamResult(new PrintStream(new FileOutputStream(filename),true, StandardCharsets.UTF_8));
你可以试一试这样,但是我这样并没有生效。
萌狼蓝天的解决方法是:
把项目编码,Java文件编码都设置成UTF-8,清除缓存重新启动即可。
萌狼-图1-修改项目编码
萌狼-图2-清除缓存
标签:XML,xml,Java,doc,文档,new,import,javax From: https://www.cnblogs.com/mllt/p/java_create_xml_file.html