您的位置:首页 > 其它

用Dom和Sax对xml文件的解析

2013-07-30 21:25 281 查看
package day12.xml;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 使用DOM解析xml文档。
*/
public class TestDOMBook {
public static void main(String[] args) {
// 1、得到DOM解析器的工厂实例
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// 2、从DOM工厂获得DOM解析器
DocumentBuilder db = dbf.newDocumentBuilder();
// 3、解析XML文档,得到一个Document,即DOM树
Document doc = db.parse("e:/book.xml");
// 4、得到所有book节点列表信息
NodeList petList = doc.getElementsByTagName("book");
// 5、轮循书本信息
System.out.println("XML文件中book的初始化信息:");
for (int i = 0; i < petList.getLength(); i++) {
// 得到book元素
Element book = (Element) petList.item(i);
// 得到book元素下的id属性的值
String strId = book.getAttributeNode("id").getNodeValue();
System.out.println("ID:" + strId);
// 得到book下的title子元素节点下的子文本节点的值
String strTitle = book.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue();
// 得到book下的title子元素节点
Element title = (Element) book.getElementsByTagName("title")
.item(0);
// 得到title元素节点的tid属性节点的值
String strTid = title.getAttributeNode("tid").getNodeValue();
String strAuthor = book.getElementsByTagName("author").item(0)
.getFirstChild().getNodeValue();
String strYear = book.getElementsByTagName("year").item(0)
.getFirstChild().getNodeValue();
String strPrice = book.getElementsByTagName("price").item(0)
.getFirstChild().getNodeValue();
System.out.println("标题:" + strTitle);
System.out.println("标题ID:" + strTid);
System.out.println("作者:" + strAuthor);
System.out.println("出版日期:" + strYear);
System.out.println("价格:" + strPrice);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}



package day12.xml;
import java.io.File;
import java.io.IOException;
import java.util.Stack;
import javax.xml.parsers.ParserConfigurationException;
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 SAXparseXml extends DefaultHandler {
private static SAXParserFactory saxParserFactory=null;
private static SAXParser saxParser=null;
private Stack<String> stack = new Stack<String>();
public static void main(String[] args) throws IOException {
File file = new File("E:"+File.separator+"Person.xml");
//1. 创建 SAXParserFactory 的实例
saxParserFactory = SAXParserFactory.newInstance();
try {
//2.创建 SAXParser 的实例
saxParser= saxParserFactory.newSAXParser();
//3.创建 SAXParserHandler类
SAXparseXml handler= new SAXparseXml();
//4.使用 parse()方法解析 XML文档
saxParser.parse(file, handler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}

@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equals("Name")) {
System.out.print("Name:"+attributes.getValue("","value"));
}else
stack.push(qName);
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (!qName.equals("Name")) {
stack.pop();
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String tmp=stack.peek();
if (tmp.equals("Age")) {
System.out.print("   age:"+new String(ch,start,length));
}
if (tmp.equals("Address")) {
System.out.println("   address:"+new String(ch,start,length));
}
}
}


本文出自 “MySpace” 博客,请务必保留此出处http://wangzhaoli.blog.51cto.com/7607113/1260867
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: