程序每運行一次,結果就會發(fā)生變化,并且還有異常,求大神解!
源代碼:
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: 前言中不允許有內(nèi)容。
?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; 前言中不允許有內(nèi)容。
?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
2016-09-02
package net.paoyun.entity;
public class Book {
// 對應的book的節(jié)點名:
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文件都要導入
2016-09-02
性能測試:
DOM:123
SAX:109
JDOM:54
DOM4J:153
這是我用你的代碼測試出來的結果