在慕课看java工程师的路径课程,看完了java的对xml文件读取与写入,在这里对这四种操作方式做个记录。这里对于DOM和SAX各自的优缺点的不进行讨论了,主要将各自的操作步骤,和解析、创建进行列举,并对他们的速度进行检测。
这4个操作xml的方式
树结构导入内存中
基于事件一步步向下解析
基于以上1和2方式,需要导入jar包
基于以上1和2方式,需要导入jar包
books.xml (对这个文件进行解析)
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>安徒生童话</name>
<author>安徒生</author>
<year>2000</year>
<price>100</price>
</book>
<book id="2">
<name>安徒生童话2</name>
<author>安徒生2</author>
<year>2002</year>
<price>102</price>
</book>
</bookstore>
Book.java (书的实体类)
public class Book {
private String id;
private String name;
private String author;
private String year;
private String price;
......
}
GenericHelper.java(通用帮助类)
每个xml的操作类中都有两个方法
public static List<Book> parse()
public static void createXML(List<Book> books)
DOM4JTest.javaDOM4J的使用
DOMTest.javaDOM的使用
JDOMTest.javaJDOM的使用
SAXTest.javaSAX的使用
XMLSeedTest.java以上的速度测试
为了节约空间以下只写出关键代码,详细解析细节就点击标题去进去吧!
具体代码: https://github.com/xujiaji/XMLTest
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("books.xml");
// 获取所有book节点的集合
NodeList bookList = document.getElementsByTagName("book");
// 遍历所有的book
//获取一个SAXParserFactory的实力
SAXParserFactory factory = SAXParserFactory.newInstance();
//通过factory获取SAXParser对象
SAXParser parser = factory.newSAXParser();
SAXParserHandler handler = new SAXParserHandler();
parser.parse("books.xml", handler);
//***********JDOM的初始步骤**********
SAXBuilder saxBuilder = new SAXBuilder();
InputStream in = new FileInputStream("books.xml");
Document document = saxBuilder.build(in);
//*****************开始解析******************
// *************DOM4J读取xml文件*******************
SAXReader reader = new SAXReader();
Document document = reader.read("books.xml");
// ****************获取根节点开始解析***************************
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// *********创建一个文档********
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.newDocument();
document.setXmlStandalone(true);// 去掉xml声明:standalone="no"
// *********添加向内存中的xml树添加内容********
Element bookstore = document.createElement("bookstore");
.......省略.....................................................
// *********输出内存中的xml树输出到文件********
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.transform(new DOMSource(document), new StreamResult(new File("NewBooks_DOM.xml")));
//***********初始化转换器************
SAXTransformerFactory stff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = stff.newTransformerHandler();
Transformer t = handler.getTransformer();
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
t.setOutputProperty(OutputKeys.INDENT, "yes");
Result result = new StreamResult(new FileOutputStream("NewBooks_SAX.xml"));
handler.setResult(result);
//***********开始向xml文件添加内容************
//开始文档
handler.startDocument();
.......省略.....................................................
// *****************设置xml内容********************
Element root = new Element("bookStore");
Element book = new Element("book");
book.setAttribute("id", "1");
.......省略.....................................................
// *****************输出xml文件********************
Format format = Format.getPrettyFormat();
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(document, new FileOutputStream("NewBooks_JDOM.xml"));
//************创建文档***************
Document document = DocumentHelper.createDocument();
//*************填充数据*********************
Element bookStore = document.addElement("bookStore");
.......省略.....................................................
// ***************输出*******************
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileOutputStream("NewBooks_DOM4J.xml"), format);
//如果不希望特殊字符被转义,那么将默认的自动转义设置为false
writer.setEscapeText(false);
writer.write(document);
writer.close();
package xml;
import java.util.List;
import xml.entity.Book;
public class XMLSeedTest {
public static void main(String[] args) {
System.out.println("*****************读取xml文件速度测试*****************");
long start = System.currentTimeMillis();
long end = 0;
DOMTest.parse();
end = System.currentTimeMillis();
xPrint("DOM解析时间", start, end);
start = System.currentTimeMillis();
SAXTest.parse();
end = System.currentTimeMillis();
xPrint("SAX解析时间", start, end);
start = System.currentTimeMillis();
JDOMTest.parse();
end = System.currentTimeMillis();
xPrint("JDOM解析时间", start, end);
start = System.currentTimeMillis();
DOM4JTest.parse();
end = System.currentTimeMillis();
xPrint("DOM4J解析时间", start, end);
System.out.println("*****************创建xml文件速度测试*****************");
List<Book> books = JDOMTest.parse();
start = System.currentTimeMillis();
DOMTest.createXML(books);
end = System.currentTimeMillis();
xPrint("DOM创建时间", start, end);
start = System.currentTimeMillis();
SAXTest.createXML(books);
end = System.currentTimeMillis();
xPrint("SAX创建时间", start, end);
start = System.currentTimeMillis();
JDOMTest.createXML(books);
end = System.currentTimeMillis();
xPrint("JDOM创建时间", start, end);
start = System.currentTimeMillis();
DOM4JTest.createXML(books);
end = System.currentTimeMillis();
xPrint("DOM4J创建时间", start, end);
}
public static void xPrint(String who, long start, long end) {
System.out.println(who + ":" + (end - start));
}
}
*****************读取xml文件速度测试*****************
DOM解析时间:38
SAX解析时间:6
JDOM解析时间:50
DOM4J解析时间:38
*****************创建xml文件速度测试*****************
DOM创建时间:40
SAX创建时间:2
JDOM创建时间:28
DOM4J创建时间:12
以上,作为XML的操作。为什么只写关键部分呢?那自然是因那部分记不住咯!
Author: @雨雪纷飞
Github: @xujiaji