java中XML格式的字符串4读取方式的简单比较

1.java自带的DOM解析.
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class ReaderXmlForDOM {

public static void main(String args[]) {
String text ="<XML><A>123</A><B>whl123</B><C>亮亮</C><D>1</D><E>1</E><F>165074</F><G>贫穷</G><H>1698.0</H><I>初级士官</I><J>湖南</J><K>常德</K><L>1</L></XML>"

long begin = System.currentTimeMillis();
parse( text );
long after = System.currentTimeMillis();
System.out.println("DOM用时"+(after-begin)+"毫秒");
}

public static void parse(String protocolXML) {

try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder
.parse(new InputSource(new StringReader(protocolXML)));

Element root = doc.getDocumentElement();
NodeList books = root.getChildNodes();
if (books != null) {
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
System.out.println("节点=" + book.getNodeName() + "\ttext="
+ book.getFirstChild().getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我当时dom解析用时是个不稳定的值在62-78毫秒之间.
以下的三种方法mian方法里面都是一样的解析那段字符串,记录用时,就不在贴出, 只贴出所用的解析部分
2.java自带的SAX解析.
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ReaderXmlForSAX {
public static void parse(String protocolXML) {

try {
SAXParserFactory saxfac = SAXParserFactory.newInstance();
SAXParser saxparser = saxfac.newSAXParser();
TestSAX tsax = new TestSAX();
saxparser.parse(new InputSource(new StringReader(protocolXML)),tsax);
} catch (Exception e) {
e.printStackTrace();
}
}
}

class TestSAX extends DefaultHandler{

private StringBuffer buf;
private String str;
public TestSAX(){
super();
}

public void startDocument() throws SAXException{
buf=new StringBuffer();
System.out.println("*******开始解析XML*******");
}

public void endDocument() throws SAXException{
System.out.println("*******XML解析结束*******");
}

public void endElement(String namespaceURI,String localName,String fullName )throws SAXException{
str = buf.toString();
System.out.println("节点="+fullName+"\tvalue="+buf+" 长度="+buf.length());
System.out.println();
buf.delete(0,buf.length());
}

public void characters( char[] chars, int start, int length )throws SAXException{
//将元素内容累加到StringBuffer中
buf.append(chars,start,length);
}
}
SAX用时是62毫秒,最快的.但是需要多写一个类
3.DOM4J解析,需要下载对应的jar包
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class ReaderXmlForDOM4J {
//DOM4j解析XML
public static void parse(String protocolXML) {

try {

Document doc=(Document)DocumentHelper.parseText(protocolXML);
Element books = doc.getRootElement();
System.out.println("根节点"+books.getName());
// Iterator users_subElements = books.elementIterator("UID");//指定获取那个元素
Iterator Elements = books.elementIterator();
while(Elements.hasNext()){
Element user = (Element)Elements.next();
System.out.println("节点"+user.getName()+"\ttext="+user.getText());
List subElements = user.elements();
// List user_subElements = user.elements("username");指定获取那个元素
// System.out.println("size=="+subElements.size());
// for( int i=0;i<subElements.size();i++){
// Element ele = (Element)subElements.get(i);
// System.out.print(ele.getName()+" : "+ele.getText()+" ");
// }
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
DOM4J的用时为140毫秒,慢 并且还需要单独的jar包
4.JDOM解析,需要下载对应的jar包
import java.io.StringReader;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.xml.sax.InputSource;

public class ReaderXmlForJDOM {
public static void parse(String protocolXML) {
SAXBuilder builder=new SAXBuilder(false);
try {
Document doc = builder.build(new InputSource(new StringReader(protocolXML)));
Element eles = doc.getRootElement(); // 得到根元素
System.out.println("根节点"+eles.getName());

List<Element> list = eles.getChildren(); // 得到元素的集合
//List studentList = students.getChildren("student"); // 得到指定元素(节点)的集合

if( list!=null){
for (int i = 0; i < list.size(); i++) {
Element book = (Element)list.get(i);
System.out.println("节点=" + book.getName() + "\ttext="
+ book.getValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
JDOM的用时为125毫秒,比较慢 并且还需要单独的jar包;