import java.io.File;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jdom.input.SAXBuilder;
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 MyXMLReader extends DefaultHandler {
@SuppressWarnings("unchecked")
java.util.Stack tags = new java.util.Stack();
public static void main(String arge[]) {
// DOM();
// SAX();
// DOM4J();
// JDOM();
// DOM4J解析xml最好
}
public MyXMLReader() {
super();
}
// DOM(JAXP Crimson解析器)
public static void DOM() {
try {
File f = new File("d:/88.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList nl = doc.getElementsByTagName("VALUE");
for (int i = 0; i < nl.getLength(); i++) {
System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
// DOM4J http://dom4j.sourceforge.net
@SuppressWarnings("unchecked")
public static void DOM4J() {
try {
File f = new File("d:/88.xml");
SAXReader reader = new SAXReader();
org.dom4j.Document doc = reader.read(f);
Element root = ((org.dom4j.Document) doc).getRootElement();
Element foo;
Iterator i = root.elementIterator("VALUE");
while (i.hasNext()) {
foo = (Element) i.next();
System.out.print("车牌号码:" + foo.elementText("NO"));
System.out.println("车主地址:" + foo.elementText("ADDR"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
// JDOM http://www.jdom.org
@SuppressWarnings("unchecked")
public static void JDOM() {
try {
SAXBuilder builder = new SAXBuilder();
org.jdom.Document doc = builder.build(new File("d:/88.xml"));
org.jdom.Element foo = doc.getRootElement();
List allChildren = foo.getChildren();
for (int i = 0; i < allChildren.size(); i++) {
System.out.print("车牌号码:" + ((org.jdom.Element) allChildren.get(i)).getChild("NO").getText());
System.out.println("车主地址:" + ((org.jdom.Element) allChildren.get(i)).getChild("ADDR").getText());
}
} catch (Exception e) {
e.printStackTrace();
}
}
// SAX
public static void SAX() {
try {
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
MyXMLReader reader = new MyXMLReader();
sp.parse(new InputSource("d:/88.xml"), reader);
} catch (Exception e) {
e.printStackTrace();
}
}
public void characters(char ch[], int start, int length) throws SAXException {
String tag = (String) tags.peek();
if (tag.equals("NO")) {
System.out.print("车牌号码:" + new String(ch, start, length));
}
if (tag.equals("ADDR")) {
System.out.println("地址:" + new String(ch, start, length));
}
}
@SuppressWarnings("unchecked")
public void startElement(String uri, String localName, String qName, Attributes attrs) {
tags.push(qName);
}
}
//88.xml 内容如下
<?xml version="1.0" encoding="GB2312"?>
<RESULT>
<VALUE>
<NO>皖A8888</NO>
<ADDR>安徽省合肥市</ADDR>
</VALUE>
<VALUE>
<NO>皖N8888</NO>
<ADDR>安徽省六安市</ADDR>
</VALUE>
</RESULT>