Book.xml
<?xml version="1.0" encoding="UTF-8"?> <books> <book isbn="33322"> <name>《水浒传》</name> <price>58</price> <author>施耐庵</author> <year>元末</year> </book> <book isbn="33323"> <name>《西游记》</name> <price>58</price> <author>吴承恩</author> <year>明代</year> </book> </books>
DOM方式解析XML:
package com.mmsiles.xml;
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 {
// 实例化doc工厂
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
// 实例化Doc构造器
DocumentBuilder builder = factory.newDocumentBuilder();
File file = new File("src/xml/Book.xml");
// 解析XML文件
Document doc = builder.parse(file);
// 获取根元素
Element root = doc.getDocumentElement();
// 获取节点列表
NodeList books = root.getChildNodes();
// 循环节点列表
for (int i = 0; i < books.getLength(); i++) {
// 获取某个节点
Node book = books.item(i);
if (book.getNodeType() == Node.ELEMENT_NODE) {
// 获取节点属性值
String isbn=book.getAttributes().getNamedItem("isbn").getNodeValue();
System.out.print("isbn :"+isbn+"\t");
//循环子节点
for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getNodeName().equals("name")){
String name=node.getFirstChild().getNodeValue();
System.out.print("名称:"+name+"\t");
}
if(node.getNodeName().equals("price")){
String price=node.getFirstChild().getNodeValue();
System.out.print("价格:"+price+"\t");
}
if(node.getNodeName().equals("author")){
String author=node.getFirstChild().getNodeValue();
System.out.print("作者:"+author+"\t");
}
if(node.getNodeName().equals("year")){
String year=node.getFirstChild().getNodeValue();
System.out.print("年份:"+year+"\n");
}
}
}
}
// System.out.println("\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
SAX方式解析XML:该种方式解析时,如果xml文档被格式化,可能存在不可见空格导致打印输出排版问题
package com.mmsiles.xml;
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.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) {
String tag = 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.print("\t" + new String(ch, start, length));
}
}
// 接受文档开始的通知
public void startElement(String uri, String localName, String qName,
Attributes attrs) {
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工厂
SAXParserFactory factory=SAXParserFactory.newInstance();
//取得SAX解析器
SAXParser parser=factory.newSAXParser();
//解析XML文件
File file=new File("src/xml/Book.xml");
TestSAX sax=new TestSAX();
parser.parse(file, sax);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println(end-start);
}
}
JDOM方式解析XML:需要JDOM的jar包支持。http://www.jdom.org
DOM4J方式解析XML:需要DOM4J的jar包支持。http://dom4j.org
package com.mmsiles.xml;
import java.io.File;
import java.util.Iterator;
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解析器
SAXReader reader = new SAXReader();
// 解析XML文件
File file = new File("src/xml/Book.xml");
Document doc = reader.read(file);
// 取得根节点
Element root = doc.getRootElement();
// 取得节点列表
for (Iterator it = root.elementIterator(); it.hasNext();) {
// 取得某一个子节点
Element book = (Element) it.next();
// 取得节点属性
String isbn = book.attributeValue("isbn");
System.out.print(isbn+"\t");
for (int i = 1; i < book.nodeCount(); i++) {
String str = book.node(i).getText();
// // 取得名称
// String name = book.node(1).getText();
// // 取得价格
// String price = book.node(2).getText();
// // 取得作者
// String author = book.node(3).getText();
// // 取得年份
// String year = book.node(4).getText();
// 打印
// System.out.print("\t" + name);
// System.out.print("\t" + price);
// System.out.print("\t" + author);
// System.out.print("\t" + year+"\n");
if(str.contains("\t")){
continue;
}
System.out.print( str+ "\t\t");
}
System.out.println();
}
} catch (Exception e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
// System.out.println(end-start);
}
}