sax解析

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
 2     <>
 3         <书名 name="xxxx">何茂赟自传</书名>
 4         <作者>何茂赟</作者>
 5         
 6    <售价>500.00</售价></>
 7     <>
 8         <书名>何茂赟自传2</书名>
 9         <作者>何茂赟</作者>
10         <售价>100.00元</售价>
11     </>
12 </书架>
  1 //得到xml文档所有内容
  2 package sax;
  3 
  4 import java.io.IOException;
  5 
  6 import javax.xml.parsers.ParserConfigurationException;
  7 import javax.xml.parsers.SAXParser;
  8 import javax.xml.parsers.SAXParserFactory;
  9 
 10 import org.xml.sax.Attributes;
 11 import org.xml.sax.ContentHandler;
 12 import org.xml.sax.Locator;
 13 import org.xml.sax.SAXException;
 14 import org.xml.sax.XMLReader;
 15 import org.xml.sax.helpers.DefaultHandler;
 16 
 17 public class sax {
 18     public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
 19         //1、创建解析工厂
 20         SAXParserFactory factory=SAXParserFactory.newInstance();
 21         //2、得到解析器
 22         SAXParser sp=factory.newSAXParser();
 23         //3、得到读取器
 24         XMLReader reader=sp.getXMLReader();
 25         //4、设置内容处理
 26         reader.setContentHandler(new ListHandler());
 27         //5、读取xml文档内容
 28         reader.parse("src/sax/shu.xml");
 29     }
 30 
 31 }
 32 //得到xml文档所有内容
 33 class ListHandler implements ContentHandler{
 34 
 35     @Override
 36     public void startElement(String uri, String localName, String name,
 37             Attributes atts) throws SAXException {
 38         System.out.println("<"+name+">");
 39         for(int i=0;atts!=null && i<atts.getLength();i++)
 40         {
 41             String attname=atts.getLocalName(i);
 42             String value=atts.getValue(i);
 43             System.out.println(attname+"="+value);
 44         }
 45         
 46     }
 47     @Override
 48     public void characters(char[] ch, int start, int length)
 49             throws SAXException {
 50         System.out.println(new String(ch,start,length));
 51         
 52     }
 53 
 54     @Override
 55     public void endElement(String uri, String localName, String name)
 56             throws SAXException {
 57         System.out.println("</"+name+">");
 58         
 59     }
 60     @Override
 61     public void endDocument() throws SAXException {
 62         // TODO Auto-generated method stub
 63         
 64     }
 65 
 66     
 67 
 68     @Override
 69     public void endPrefixMapping(String prefix) throws SAXException {
 70         // TODO Auto-generated method stub
 71         
 72     }
 73 
 74     @Override
 75     public void ignorableWhitespace(char[] ch, int start, int length)
 76             throws SAXException {
 77         // TODO Auto-generated method stub
 78         
 79     }
 80 
 81     @Override
 82     public void processingInstruction(String target, String data)
 83             throws SAXException {
 84         // TODO Auto-generated method stub
 85         
 86     }
 87 
 88     @Override
 89     public void setDocumentLocator(Locator locator) {
 90         // TODO Auto-generated method stub
 91         
 92     }
 93 
 94     @Override
 95     public void skippedEntity(String name) throws SAXException {
 96         // TODO Auto-generated method stub
 97         
 98     }
 99 
100     @Override
101     public void startDocument() throws SAXException {
102         // TODO Auto-generated method stub
103         
104     }
105 
106     
107 
108     @Override
109     public void startPrefixMapping(String prefix, String uri)
110             throws SAXException {
111         // TODO Auto-generated method stub
112         
113     }
114     
115 }
 1 //获取xml文档指定位置的值
 2 package sax;
 3 
 4 import java.io.IOException;
 5 
 6 import javax.xml.parsers.ParserConfigurationException;
 7 import javax.xml.parsers.SAXParser;
 8 import javax.xml.parsers.SAXParserFactory;
 9 
10 import org.xml.sax.Attributes;
11 import org.xml.sax.ContentHandler;
12 import org.xml.sax.Locator;
13 import org.xml.sax.SAXException;
14 import org.xml.sax.XMLReader;
15 import org.xml.sax.helpers.DefaultHandler;
16 
17 public class sax1 {
18     public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
19         //1、创建解析工厂
20         SAXParserFactory factory=SAXParserFactory.newInstance();
21         //2、得到解析器
22         SAXParser sp=factory.newSAXParser();
23         //3、得到读取器
24         XMLReader reader=sp.getXMLReader();
25         //4、设置内容处理
26         reader.setContentHandler(new TagValueHandler());
27         //5、读取xml文档内容
28         reader.parse("src/sax/shu.xml");
29     }
30 
31 }
32 //获取xml文档指定位置的值
33 class TagValueHandler extends DefaultHandler
34 {
35 
36     private String currentTag;   //记住当前解析到的是什么标签
37     private int needNumber=2;
38     private int currentNumber;
39     @Override
40     public void startElement(String uri, String localName, String name,
41             Attributes attributes) throws SAXException {
42         currentTag=name;
43         if(currentTag.equals("作者"))
44         {
45             currentNumber++;
46         }
47     }
48     @Override
49     public void characters(char[] ch, int start, int length)
50             throws SAXException {
51         if("作者".equals(currentTag)&&currentNumber==needNumber)
52         {
53             System.out.print(new String(ch,start,length));
54         }
55     }
56 
57     @Override
58     public void endElement(String uri, String localName, String qName)
59             throws SAXException {
60         currentTag=null;
61     }
62 
63     
64     
65 }

 

posted on 2015-02-01 16:02  曾经的码农  阅读(413)  评论(0编辑  收藏  举报