本文来自于163.com/blog/static/9732381120118303351185/
使用DOM读取XML文件:
package test;
import java.io.File;
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;
public class TestDOM {
public static void main(String args[]) {
long start =System.currentTimeMillis();
try {
//取得DOM工厂
DocumentBuilderFactoryfactory = DocumentBuilderFactory
.newInstance();
//取得DOM解析器
DocumentBuilderbuilder = factory.newDocumentBuilder();
//解析文件
File file =new File("src/books.xml");
Document doc= builder.parse(file);
//取得根节点
Element root= doc.getDocumentElement();
//取得子节点列表
NodeListbooks = root.getChildNodes();
for (int i =0; i < books.getLength(); i++) {
//取得某一个子节点
Nodebook = books.item(i);
if(book.getNodeType() == Node.ELEMENT_NODE) {
//取得属性值
Stringisbn = book.getAttributes().getNamedItem("isbn")
.getNodeValue();
System.out.print(isbn);
//轮循子节点
for(Node node = book.getFirstChild(); node != null; node = node
.getNextSibling()){
if(node.getNodeType() == Node.ELEMENT_NODE) {
if(node.getNodeName().equals("name")) {
Stringname = node.getFirstChild()
.getNodeValue();
System.out.print("\t"+ name);
}
if(node.getNodeName().equals("price")) {
Stringprice = node.getFirstChild()
.getNodeValue();
System.out.print("\t"+ price);
}
if(node.getNodeName().equals("author")) {
Stringauthor = node.getFirstChild()
.getNodeValue();
System.out.print("\t"+ author);
}
if(node.getNodeName().equals("year")) {
Stringyear = node.getFirstChild()
.getNodeValue();
System.out.println("\t"+ year);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
long end =System.currentTimeMillis();
System.out.println(end -start);
}
}
使用SAX读取XML文件:
package test;
import java.io.File;
import java.util.Stack;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class TestSAX extends DefaultHandler {
Stack<String> tags= new Stack<String>();
public TestSAX() {
super();
}
public void characters(char ch[], int start,int length)
throwsSAXException {
String tag = (String)tags.peek();
if (tag.equals("name")) {
System.out.print("\t"+ new String(ch, start, length));
}
if (tag.equals("price")){
System.out.print("\t"+ new String(ch, start, length));
}
if (tag.equals("author")){
System.out.print("\t"+ new String(ch, start, length));
}
if (tag.equals("year")) {
System.out.println("\t"+ new String(ch, start, length));
}
}
public void startElement(String uri, StringlocalName, String qName,
Attributesattrs) {
if (qName.equals("book")){
System.out.print(attrs.getValue("isbn"));
}
tags.push(qName);
}
public static void main(String args[]) {
long start =System.currentTimeMillis();
try {
//取得SAX工厂
SAXParserFactoryfactory = SAXParserFactory.newInstance();
//取得SAX解析器
SAXParserparser = factory.newSAXParser();
//解析文件
File file =new File("src/books.xml");
TestSAX sax =new TestSAX();
parser.parse(file,sax);
} catch (Exception e){
e.printStackTrace();
}
long end =System.currentTimeMillis();
System.out.println(end -start);
}
}
使用JDOM读写XML:
package test;
import java.io.File;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class TestJDOM {
public static void main(String args[]) {
long start =System.currentTimeMillis();
try {
//取得解析器
SAXBuilderbuilder = new SAXBuilder();
//解析文件
File file =new File("src/books.xml");
Document doc= builder.build(file);
//取得根节点
Element root= doc.getRootElement();
//取得子节点列表
List<?>books = root.getChildren();
for (int i =0; i < books.size(); i++) {
//取得某一个子节点
Elementbook = (Element)books.get(i);
//取得属性值
Stringisbn = book.getAttributeValue("isbn");
System.out.print(isbn);
Stringname = book.getChild("name").getText();
Stringprice = book.getChild("price").getText();
Stringauthor = book.getChild("author").getText();
Stringyear = book.getChild("year").getText();
System.out.print("\t"+ name);
System.out.print("\t"+ price);
System.out.print("\t"+ author);
System.out.println("\t"+ year);
}
} catch (Exception e) {
e.printStackTrace();
}
long end =System.currentTimeMillis();
System.out.println(end -start);
}
}
使用DOM4J读写XML文件:
package test;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class TestDOM4J {
public static void main(String args[]) {
long start =System.currentTimeMillis();
try {
//取得SAX解析器
SAXReaderreader = new SAXReader();
//解析文件
File file =new File("src/books.xml");
Document doc= reader.read(file);
//取得根节点
Element root= doc.getRootElement();
//取得子节点列表
for (int i =0; i < root.nodeCount(); i++) {
//取得某一个子节点
Elementbook = (Element) root.node(i);
//取得属性值
Stringisbn = book.attributeValue("isbn");
System.out.print(isbn);
Stringname = book.node(0).getText();
Stringprice = book.node(1).getText();
Stringauthor = book.node(2).getText();
Stringyear = book.node(3).getText();
System.out.print("\t"+ name);
System.out.print("\t"+ price);
System.out.print("\t"+ author);
System.out.println("\t"+ year);
}
} catch (Exception e) {
e.printStackTrace();
}
long end =System.currentTimeMillis();
System.out.println(end -start);
}
}