SAX解析xml简单实例
SAX解析XML简单实例
2007-11-11 00:36
SAX主要是以串行和层次的方式进行解析,如下是一个很简单的解析例子: 1、选择一个要解析的xml文档 <?xml version="1.0"?> <!DOCTYPE book SYSTEM "DTD/JavaXML.dtd"> <!-- Java and XML Contents --> <book xmlns="http://www.oreilly.com/javaxml2" xmlns:ora="http://www.oreilly.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.oreilly.com/javaxml2 XSD/contents.xsd http://www.oreilly.com XSD/contents-ora.xsd"> <title ora:series="Java">Java and XML</title> <!-- Chapter List --> <contents> <chapter title="Introduction" number="1"> <topic name="XML Matters"/> <topic name="What's Important"/> <topic name="The Essentials"/> <topic name="What's Next?"/> </chapter> <chapter title="Nuts and Bolts" number="2"> <topic name="The Basics"/> <topic name="Constraints"/> <topic name="Transformations"/> <topic name="And More..."/> <topic name="What's Next?"/> </chapter> <chapter title="SAX" number="3"> <topic name="Getting Prepared"/> <topic name="SAX Readers"/> <topic name="Content Handlers"/> <topic name="Gotcha!"/> <topic name="What's Next?"/> </chapter> </contents> </book> 2、使用SAX进行解析,代码如下: package javaxml2; import java.io.IOException; import java.io.OutputStreamWriter; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.InputSource; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; public class MySaxXml extends DefaultHandler{ /** * @param args * @throws SAXException * @throws Exception */ private Locator locator; private int index1=0; private int index2=0; public void characters(char[] ch, int start, int length) throws SAXException { // TODO 自动生成方法存根 //System.out.print( "SAX Event: CHARACTERS[ " ); System.out.print("本书<<"); try { OutputStreamWriter outw = new OutputStreamWriter(System.out); outw.write( ch, start,length ); outw.flush(); } catch (Exception e) { e.printStackTrace(); } System.out.println(">>的目录"); //System.out.println( " ]" ); } public void endDocument() throws SAXException { // TODO 自动生成方法存根 System.out.println( "解析结束:" ); } public void endElement(String uri, String localName, String qName) throws SAXException { // TODO 自动生成方法存根 //System.out.println( "SAX Event: END ELEMENT[ " + localName + " ]" ); //this.index2=0; if(qName.equalsIgnoreCase("chapter")) this.index2=0; } public void setDocumentLocator(Locator locator) { // TODO 自动生成方法存根 this.locator=locator; } public void startDocument() throws SAXException { // TODO 自动生成方法存根 System.out.println( "解析开始: " ); } public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { // TODO 自动生成方法存根 //System.out.println( "SAX Event: START ELEMENT[ " + localName + " ]" ); if(qName.equalsIgnoreCase("chapter")){ index1++; for ( int i = 0; i < atts.getLength(); i++ ){ //System.out.println(atts.getLength()); String attName=atts.getQName(i); if(attName.equalsIgnoreCase("title")){ System.out.println("第"+index1+"章:"+atts.getValue(i)); } } } if(qName.equalsIgnoreCase("topic")){ index2++; for ( int i = 0; i < atts.getLength(); i++ ){ String attName=atts.getQName(i); if(attName.equalsIgnoreCase("name")){ System.out.println(" 第"+index2+"部分:"+atts.getValue(i)); } } } } public static void main(String[] args) throws Exception { // TODO 自动生成方法存根 String vendorParser String xmlURI="http://localhost:8080/TestXml/contents.xml"; XMLReader reader = XMLReaderFactory.createXMLReader(vendorParserClass); reader.setContentHandler(new MySaxXml()); InputSource inputSource=new InputSource(xmlURI); reader.parse(inputSource); } } 3、其中contentHandler只实现了它的几个方法,解析结果如下: 解析开始: 本书<<Java and XML>>的目录 第1章:Introduction 第1部分:XML Matters 第2部分:What's Important 第3部分:The Essentials 第4部分:What's Next? 第2章:Nuts and Bolts 第1部分:The Basics 第2部分:Constraints 第3部分:Transformations 第4部分:And More... 第5部分:What's Next? 第3章:SAX 第1部分:Getting Prepared 第2部分:SAX Readers 第3部分:Content Handlers 第4部分:Gotcha! 第5部分:What's Next? |
评论这张
转发至微博
转发至微博
历史上的今天
相关文章
- Android读写XML(中)——SAX2010-07-30 14:19:42
- 【引用】Android SAX 方式解析XML 字符串2011-01-06 13:01:32
- java中四种操作(DOM、SAX、JDOM、 DOM4J)xml方式详解与比较(ZT)2010-06-06 11:21:05
- XML解析2011-03-06 19:43:52
- Java解析XML文档——dom解析xml-Java频道-中国IT实验室2010-12-12 03:32:45
评论