慕粉3170877
2016-09-01 16:48
源代码:
package com.imooc.parseTest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.jdom2.Attribute;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ParseTest {
public void domXmlParser() {
ArrayList<Book> bookLists = new ArrayList<Book>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse("f:\\books.xml");
NodeList bookList = document.getElementsByTagName("book");
for (int i = 0; i < bookList.getLength(); i++) {
Node book = bookList.item(i);
Book bookEntity = new Book();
NamedNodeMap attrs = book.getAttributes();
for (int j = 0; j < attrs.getLength(); j++) {
Node attr = attrs.item(j);
if (attr.getNodeName().equals("id")) {
bookEntity.setID(attr.getNodeValue());
}
}
NodeList childNodes = book.getChildNodes();
for (int k = 0; k < childNodes.getLength(); k++) {
if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) {
String name = childNodes.item(k).getNodeName();
String value = childNodes.item(k).getFirstChild().getNodeValue();
if (name.equals("name")) {
bookEntity.setName(value);
}
else if (name.equals("author")) {
bookEntity.setAuthor(value);
}
else if (name.equals("year")) {
bookEntity.setYear(value);
}
else if (name.equals("price")) {
bookEntity.setPrice(value);
}
else if (name.equals("language")) {
bookEntity.setLanguage(value);
}
}
}
bookLists.add(bookEntity);
bookEntity = null;
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void saxXmlParser(){
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
SAXParseHandler handler = new SAXParseHandler();
parser.parse("f:\\books.xml", handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void jdomXmlParser() {
ArrayList<Book> booksList = new ArrayList<Book>();
SAXBuilder saxBuilder = new SAXBuilder();
InputStream in;
try {
in = new FileInputStream("f:\\books.xml");
InputStreamReader isr = new InputStreamReader(in, "UTF-8");
org.jdom2.Document document = saxBuilder.build(isr);
org.jdom2.Element rootElement = document.getRootElement();
List<org.jdom2.Element> bookList = rootElement.getChildren();
for (org.jdom2.Element book : bookList) {
Book bookEntity = new Book();
List<Attribute> attrList = book.getAttributes();
for (Attribute attr : attrList) {
String attrName = attr.getName();
String attrValue = attr.getValue();
if (attrName.equals("id")) {
bookEntity.setID(attrValue);
}
}
List<org.jdom2.Element> bookChilds = book.getChildren();
for (org.jdom2.Element child : bookChilds) {
if (child.getName().equals("name")) {
bookEntity.setName(child.getValue());
}
else if (child.getName().equals("author")) {
bookEntity.setAuthor(child.getValue());
}
else if (child.getName().equals("year")) {
bookEntity.setYear(child.getValue());
}
else if (child.getName().equals("price")) {
bookEntity.setPrice(child.getValue());
}
else if (child.getName().equals("language")) {
bookEntity.setLanguage(child.getValue());
}
}
booksList.add(bookEntity);
bookEntity = null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void dom4jXmlParser(){
ArrayList<Book> booksList = new ArrayList<Book>();
SAXReader reader = new SAXReader();
try {
org.dom4j.Document document = reader.read(new File("f:\\books.xml"));
org.dom4j.Element bookStore = document.getRootElement();
List<org.dom4j.Element> bookEles = bookStore.elements();
for (org.dom4j.Element book : bookEles) {
Book bookEntity = new Book();
List<org.dom4j.Attribute> bookAttrs = book.attributes();
for (org.dom4j.Attribute attr : bookAttrs) {
if (attr.getName().equals("id")) {
bookEntity.setID(attr.getValue());
}
}
List<org.dom4j.Element> bookss = book.elements();
for (org.dom4j.Element bookChild : bookss) {
String name = bookChild.getName();
String value = bookChild.getStringValue();
if (name.equals("name")) {
bookEntity.setName(value);
}
else if (name.equals("author")) {
bookEntity.setAuthor(value);
}
else if (name.equals("year")) {
bookEntity.setYear(value);
}
else if (name.equals("price")) {
bookEntity.setPrice(value);
}
else if (name.equals("language")) {
bookEntity.setLanguage(value);
}
}
booksList.add(bookEntity);
bookEntity = null;
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
@Test
public void testPerformance() throws Exception{
System.out.println("性能测试:");
//测试DOM的性能:
long start = System.currentTimeMillis();
domXmlParser();
System.out.println("DOM:"+ (System.currentTimeMillis() - start) );
//测试SAX的性能:
start = System.currentTimeMillis();
saxXmlParser();
System.out.println("SAX:"+ (System.currentTimeMillis() - start) );
//测试JDOM的性能:
start = System.currentTimeMillis();
jdomXmlParser();
System.out.println("JDOM:"+ (System.currentTimeMillis() - start) );
//测试DOM4J的性能:
start = System.currentTimeMillis();
dom4jXmlParser();
System.out.println("DOM4J:"+ (System.currentTimeMillis() - start) );
}
}
运行结果:
性能测试:
DOM:29
SAX:4
org.jdom2.input.JDOMParseException: Error on line 1: 前言中不允许有内容。
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:232)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:303)
at org.jdom2.input.SAXBuilder.build(SAXBuilder.java:1196)
at com.imooc.parseTest.ParseTest.jdomXmlParser(ParseTest.java:105)
at com.imooc.parseTest.ParseTest.testPerformance(ParseTest.java:205)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1437)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:999)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
at org.jdom2.input.sax.SAXBuilderEngine.build(SAXBuilderEngine.java:217)
... 27 more
JDOM:56
DOM4J:46
package net.paoyun.test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; import org.jdom2.Attribute; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; import org.jdom2.input.sax.SAXHandler; import org.junit.Test; import org.w3c.dom.DOMException; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import net.paoyun.entity.Book; public class ParseTest { public void domXmlParser() { ArrayList<Book> bookLists = new ArrayList<Book>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse("f:\\books.xml"); NodeList bookList = document.getElementsByTagName("book"); for (int i = 0; i < bookList.getLength(); i++) { Node book = bookList.item(i); Book bookEntity = new Book(); NamedNodeMap attrs = book.getAttributes(); for (int j = 0; j < attrs.getLength(); j++) { Node attr = attrs.item(j); if (attr.getNodeName().equals("id")) { bookEntity.setId(attr.getNodeValue()); } } NodeList childNodes = book.getChildNodes(); for (int k = 0; k < childNodes.getLength(); k++) { if (childNodes.item(k).getNodeType() == Node.ELEMENT_NODE) { String name = childNodes.item(k).getNodeName(); String value = childNodes.item(k).getFirstChild().getNodeValue(); if (name.equals("name")) { bookEntity.setName(value); } else if (name.equals("author")) { bookEntity.setAuthor(value); } else if (name.equals("year")) { bookEntity.setYear(value); } else if (name.equals("price")) { bookEntity.setPrice(value); } else if (name.equals("language")) { bookEntity.setLanguage(value); } } } bookLists.add(bookEntity); bookEntity = null; } } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void saxXmlParser(){ SAXParserFactory factory = SAXParserFactory.newInstance(); try { SAXParser parser = factory.newSAXParser(); SAXHandler handler = new SAXHandler(); parser.parse("f:\\books.xml", handler); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void jdomXmlParser() { ArrayList<Book> booksList = new ArrayList<Book>(); SAXBuilder saxBuilder = new SAXBuilder(); InputStream in; try { in = new FileInputStream("f:\\books.xml"); InputStreamReader isr = new InputStreamReader(in, "UTF-8"); org.jdom2.Document document = saxBuilder.build(isr); org.jdom2.Element rootElement = document.getRootElement(); List<org.jdom2.Element> bookList = rootElement.getChildren(); for (org.jdom2.Element book : bookList) { Book bookEntity = new Book(); List<Attribute> attrList = book.getAttributes(); for (Attribute attr : attrList) { String attrName = attr.getName(); String attrValue = attr.getValue(); if (attrName.equals("id")) { bookEntity.setId(attrValue); } } List<org.jdom2.Element> bookChilds = book.getChildren(); for (org.jdom2.Element child : bookChilds) { if (child.getName().equals("name")) { bookEntity.setName(child.getValue()); } else if (child.getName().equals("author")) { bookEntity.setAuthor(child.getValue()); } else if (child.getName().equals("year")) { bookEntity.setYear(child.getValue()); } else if (child.getName().equals("price")) { bookEntity.setPrice(child.getValue()); } else if (child.getName().equals("language")) { bookEntity.setLanguage(child.getValue()); } } booksList.add(bookEntity); bookEntity = null; } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void dom4jXmlParser(){ ArrayList<Book> booksList = new ArrayList<Book>(); SAXReader reader = new SAXReader(); try { org.dom4j.Document document = reader.read(new File("f:\\books.xml")); org.dom4j.Element bookStore = document.getRootElement(); List<org.dom4j.Element> bookEles = bookStore.elements(); for (org.dom4j.Element book : bookEles) { Book bookEntity = new Book(); List<org.dom4j.Attribute> bookAttrs = book.attributes(); for (org.dom4j.Attribute attr : bookAttrs) { if (attr.getName().equals("id")) { bookEntity.setId(attr.getValue()); } } List<org.dom4j.Element> bookss = book.elements(); for (org.dom4j.Element bookChild : bookss) { String name = bookChild.getName(); String value = bookChild.getStringValue(); if (name.equals("name")) { bookEntity.setName(value); } else if (name.equals("author")) { bookEntity.setAuthor(value); } else if (name.equals("year")) { bookEntity.setYear(value); } else if (name.equals("price")) { bookEntity.setPrice(value); } else if (name.equals("language")) { bookEntity.setLanguage(value); } } booksList.add(bookEntity); bookEntity = null; } } catch (DOMException e) { e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testPerformance() throws Exception{ System.out.println("性能测试:"); //测试DOM的性能: long start = System.currentTimeMillis(); domXmlParser(); System.out.println("DOM:"+ (System.currentTimeMillis() - start) ); //测试SAX的性能: start = System.currentTimeMillis(); saxXmlParser(); System.out.println("SAX:"+ (System.currentTimeMillis() - start) ); //测试JDOM的性能: start = System.currentTimeMillis(); jdomXmlParser(); System.out.println("JDOM:"+ (System.currentTimeMillis() - start) ); //测试DOM4J的性能: start = System.currentTimeMillis(); dom4jXmlParser(); System.out.println("DOM4J:"+ (System.currentTimeMillis() - start) ); } } package net.paoyun.handler; import java.util.ArrayList; import javax.xml.namespace.QName; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import net.paoyun.entity.Book; public class SAXHanlder extends DefaultHandler { // 要遍历数量的下标 // int index = 0; String value = null; Book book = null; ArrayList<Book> bookList = new ArrayList<Book>(); public ArrayList<Book> getBookList() { return bookList; } /** * 用来遍历xml的开始标签。。可以获得属性名和属性值 */ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { // TODO Auto-generated method stub // 调用父类的startElement super.startElement(uri, localName, qName, attributes); // 自定义属性的遍历 if (qName.equals("book")) { // index++; // 已知标签的属性名称,已知属性名称获取属性值 /* * String value = attributes.getValue("id"); * System.out.println("属性值:"+value); */ book = new Book(); // 不知道属性的名称和个数 int num = attributes.getLength(); for (int i = 0; i < num; i++) { // 有几个属性名就循环几次 得到属性名 System.out.println(attributes.getQName(i)); // 获取属性值,通过index的下标获取 System.out.println(attributes.getValue(i)); if (attributes.getQName(i).equals("id")) { book.setId(attributes.getValue(i)); } } } else if (!qName.equals("book") && !qName.equals("bookstore")) { System.out.println("节点名是:" + qName); } } /** * 用来遍历xml的结束标签 */ @Override public void endElement(String uri, String localName, String qName) throws SAXException { // TODO Auto-generated method stub super.endElement(uri, localName, qName); // 是否针对遍历的标签是否结束 if (qName.equals("book")) { bookList.add(book); book = null; } else if (qName.equals("id")) { book.setId(value); } else if (qName.equals("name")) { book.setName(value); } else if (qName.equals("author")) { book.setAuthor(value); } else if (qName.equals("year")) { book.setYear(value); } else if (qName.equals("price")) { book.setPrice(value); } else if (qName.equals("qitq")) { book.setQitq(value); } else if (qName.equals("language")) { book.setLanguage(value); } } /** * 用来标志解析开始 */ @Override public void startDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("解析开始"); super.startDocument(); } /** * 用来标记解析结束 */ @Override public void endDocument() throws SAXException { // TODO Auto-generated method stub System.out.println("解析结束"); super.endDocument(); } /** * 获取节点值中的内容 */ @Override public void characters(char[] ch, int start, int length) throws SAXException { // TODO Auto-generated method stub super.characters(ch, start, length); // 得到节点值的内容 value = new String(ch, start, length); // 去掉空格和多余的换行 if (!value.trim().equals("")) { System.out.println("节点值是:" + value); } } }
package net.paoyun.entity;
public class Book {
// 对应的book的节点名:
private String id;
private String name;
private String author;
private String year;
private String price;
private String qitq;
private String language;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getQitq() {
return qitq;
}
public void setQitq(String qitq) {
this.qitq = qitq;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", year=" + year + ", price=" + price
+ ", qitq=" + qitq + ", language=" + language + "]";
}
}
把需要用到的jar文件都要导入
性能测试:
DOM:123
SAX:109
JDOM:54
DOM4J:153
这是我用你的代码测试出来的结果
Java眼中的XML---文件读取
83199 学习 · 431 问题
相似问题