一、DOM
package leif;
import java.io.File;
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 org.w3c.dom.Document;
import org.w3c.dom.Element;
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
createXml();
long end = System.currentTimeMillis();
System.out.println("运行时间:" + (end - start));
}
public static void createXml() {
try {
// 1、创建DocumentBuilder解析器
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
// 2、创建DOM对象
Document document = documentBuilder.newDocument();
document.setXmlStandalone(true);// 不显示standalone="no"
// 3、创建元素节点
Element bookStore = document.createElement("bookStore");
Element book = document.createElement("book");
Element name = document.createElement("name");
// 4、创建属性节点
book.setAttribute("id", "1");
// 5、创建文本节点
name.setTextContent("Java从入门到放弃");
// 6、添加节点关系
book.appendChild(name);
bookStore.appendChild(book);
document.appendChild(bookStore);
// 7、创建Transformer转换器
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// 8、设置转换格式
transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 换行
// 9、创建XML文件并写入内容
transformer.transform(new DOMSource(document), new StreamResult(new File("C:\\Users\\Administrator\\Desktop\\bookStore.xml")));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
二、SAX
package leif;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
createXml();
long end = System.currentTimeMillis();
System.out.println("运行时间:" + (end - start));
}
public static void createXml() {
try {
// 1、创建TransformerHandler处理器
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
// 2、创建Transformer转换器并设置转换格式
Transformer transformer = transformerHandler.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");// 编码
transformer.setOutputProperty(OutputKeys.INDENT, "yes");// 换行
// 3、创建Result对象并连接XML文件
Result result = new StreamResult(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\bookStore.xml")));
// 4、关联Result到TransformerHandler处理器
transformerHandler.setResult(result);
// 5、打开DOM对象
transformerHandler.startDocument();
// 6、创建属性列表
AttributesImpl attributesImpl = new AttributesImpl();
// 7、创建元素节点和属性节点
attributesImpl.clear();
transformerHandler.startElement(null, null, "bookStore", attributesImpl);
// 8、添加子节点和子节点的属性节点
attributesImpl.clear();
attributesImpl.addAttribute(null, null, "id", null, "1");
transformerHandler.startElement(null, null, "book", attributesImpl);
attributesImpl.clear();
transformerHandler.startElement(null, null, "name", attributesImpl);
transformerHandler.characters("Java从入门到放弃".toCharArray(), 0, "Java从入门到放弃".length());
// 8、依次关闭节点
transformerHandler.endElement(null, null, "name");
transformerHandler.endElement(null, null, "book");
transformerHandler.endElement(null, null, "bookstore");
// 9、关闭DOM对象
transformerHandler.endDocument();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
三、JDOM
package leif;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
createXml();
long end = System.currentTimeMillis();
System.out.println("运行时间:" + (end - start));
}
public static void createXml() {
// 1、创建根节点
Element rootElement = new Element("bookStore");
// 2、创建DOM对象
Document document = new Document(rootElement);
// 3、创建子节点
Element bookElement = new Element("book");
Element nameElement = new Element("name");
// 4、创建属性节点
bookElement.setAttribute("id", "1");
// 5、创建文本节点
nameElement.setText("Java从入门到放弃");
// 6、添加节点到父节点
bookElement.addContent(nameElement);
rootElement.addContent(bookElement);
// 7、设置转换格式
Format format = Format.getCompactFormat();
format.setIndent(" ");// 换行和缩进
format.setEncoding("UTF-8");// 编码
// 8、创建XMLOutputter转换器
XMLOutputter xmlOutputer = new XMLOutputter(format);
// 9、转换成XML文件
try {
xmlOutputer.output(document, new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\bookStore.xml")));
} catch (IOException e) {
e.printStackTrace();
}
}
}
四、DOM4J
package leif;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
createXml();
long end = System.currentTimeMillis();
System.out.println("运行时间:" + (end - start));
}
public static void createXml() {
try {
// 1、创建DOM对象
Document document = DocumentHelper.createDocument();
// 2、创建根节点
Element rootElement = document.addElement("bookStore");
// 3、创建子节点
Element bookElement = rootElement.addElement("book");
Element nameElement = bookElement.addElement("name");
// 4、创建属性节点
bookElement.addAttribute("id", "1");
// 5、创建文本节点
nameElement.setText("Java从入门到放弃");
// 6、设置转换格式
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("UTF-8");// 编码
// 7、创建XMLWriter转换器
XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\bookStore.xml")), outputFormat);
// 8、生成XML文件
xmlWriter.write(document);
// 9、关闭XMLWriter转换器
xmlWriter.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}