用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
相关文章推荐
- DOM、SAX、JDOM、DOM4J以及PULL在XML文件解析中的工作原理以及优缺点对比
- 在android开发中,经常用到去解析xml文件,常见的解析xml的方式有一下三种:SAX、Pull、Dom解析方式。最近做了一个android版的CSDN阅读器,用到了其中的两种(sax,pull)
- Android 解析XML文件的三种方式 DOM,SAX,PULL
- java解析xml文件的2种方法:sax,dom
- java中解析xml文件的五种常见方法:DOM4J,dom,pull,SAX,Jdom
- Java基础系列17:使用DOM、SAX、JDOM、DOM4J解析XML文件详解
- 使用SAX或者DOM或者pull解析XML文件的使用
- 解析xml文件的三种方式(sax ,dom ,dom4j)
- Android三种解析xml文件的方法:SAX, PULL, DOM
- Android中Sax解析与Dom解析xml文件
- android应用开发之利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- DOM、SAX、JDOM、DOM4J以及PULL在XML文件解析中的工作原理以及优缺点对比
- android学习——使用SAX、DOM 和 PULL 解析xml文件,及使用pull生成xml文件
- Java解析XML文件的DOM和SAX方式
- Java乔晓松-使用SAX,DOM,pull解析XML文件以及pull解析生成xml文件
- Android 解析XML文件的三种方式 DOM,SAX,PULL
- Java解析XML文件的DOM和SAX方式
- Dom、Sax、JDOM和Dom4j四种方式解析XML文件小Demo和四种方式的大概比较
- 个人安卓学习笔记---分别利用SAX、DOM和Pull实现对XML文件的解析并进行单元测试
- 使用SAX或者DOM解析XML文件