前端之家收集整理的这篇文章主要介绍了
XMLParse 读取XML字段 -- JDOM & DOM4J,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.study;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Stack;
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.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class XMLParse extends DefaultHandler {
/**
* @param args
*/
Stack<String> tags = new Stack<String>();
public XMLParse() {
super();
}
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
XMLParse myXML = new XMLParse();
//System.out.println("=====================DOM=========================");
//myXML.DOM();
//System.out.println("=====================SAX=========================");
//myXML.SAX();
//System.out.println("=====================JDOM========================");
//myXML.JDOM();
//System.out.println("=====================DOM4J=======================");
myXML.DOM4J();
//System.out.println("=================================================");
}
File f = new File("D://test.xml");
/* public void DOM() {
long start = System.currentTimeMillis();
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("data");
for (int i = 0; i < nl.getLength(); i++) {
System.out.println("Name|"
+ doc.getElementsByTagName("name").item(i)
.getFirstChild().getNodeValue());
System.out.println("Mail|"
+ doc.getElementsByTagName("mail").item(i)
.getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("dom runtime" + (end - start) + "MS");
}
public void SAX() {
long start = System.currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
XMLParse reader = new XMLParse();
sp.parse(f,reader);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("sax runtime"
+ (System.currentTimeMillis() - start) + " MS");
}
public void startElement(String uri,String localName,String qName,Attributes attrs) {
tags.push(qName);
}
public void characters(char ch[],int start,int length)
throws SAXException {
String tag = (String) tags.peek();
if (tag.equals("name")) {
System.out.println("Name|" + new String(ch,start,length));
}
else if (tag.equals("mail")) {
System.out.println("Space|" + new String(ch,length));
}
}*/
public void JDOM() {
long start = System.currentTimeMillis();
try {
SAXBuilder builder = new SAXBuilder();
org.jdom2.Document doc = builder.build(f);
Element foo = doc.getRootElement();
List<?> allChildren = foo.getChildren("datas");
//Element ele=foo.getChild("datas"); //获取datas元素,如果有多个获取List个数循环查询
for (int i = 0; i < allChildren.size(); i++) {
Element ele = (Element) allChildren.get(i);
List<?> elechilds = ele.getChildren("data");
for (int j = 0; j < elechilds.size(); j++) {
System.out.println("Name | "+((Element) elechilds.get(j)).getChildText("name"));
System.out.println("Mail | "+((Element) elechilds.get(j)).getChildText("mail"));
System.out.println("QQ | "+((Element) elechilds.get(j)).getChildText("qq"));
System.out.println("-----------------");
}
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("jdom runtime" + (end - start) + "MS");
}
public void DOM4J() {
long start = System.currentTimeMillis();
try {
SAXReader reader = new SAXReader();
org.dom4j.Document doc = reader.read(f);
org.dom4j.Element root = doc.getRootElement();
for (@SuppressWarnings("rawtypes") java.util.Iterator i = root.elementIterator("datas"); i.hasNext();) {
org.dom4j.Element foo = (org.dom4j.Element) i.next();
for (@SuppressWarnings("rawtypes") java.util.Iterator j = foo.elementIterator("data"); j.hasNext();) {
org.dom4j.Element ele = (org.dom4j.Element) j.next();
System.out.println("Name|" + ((org.dom4j.Element) ele).elementText("name"));
}
System.out.println("------------------");
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println("dom4j runtime" + (end - start) + "MS");
}
}
原文链接:https://www.f2er.com/xml/300438.html