XML 文件的操作(五)用sax解析xml 读取xml

  1. <?xml version="1.0" encoding="gb2312"?>  
  2.   
  3. <?xml-stylesheet type="text/xsl" href="students.xsl"?>  
  4.   
  5. <students>  
  6.     <student sn="01">  
  7.         <name>张三</name>  
  8.         <age>18</age>  
  9.     </student>  
  10.        
  11.     <student sn="02">  
  12.         <name>李四</name>  
  13.         <age>20</age>  
  14.     </student>  
  15. </students>  

使用SAX读取XML文件:
Java代码 复制代码
  1. package com.ibm.xml;   
  2.   
  3. import java.io.File;   
  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.SAXException;   
  12. import org.xml.sax.helpers.DefaultHandler;   
  13.   
  14.   
  15. /**  
  16.  * 使用SAX读取XML文件  
  17.  * @author Administrator  
  18.  *  
  19.  */  
  20. public class SAXPrinter extends DefaultHandler {   
  21.   
  22.     /**  
  23.      * 重新的方法  
  24.      */  
  25.     public void startDocument() throws SAXException   
  26.     {   
  27.         System.out.println("<?xml version=\"1.0\" encoding='gb2312'?>");   
  28.     }   
  29.        
  30.     /**  
  31.      * 重新的方法  
  32.      */  
  33.     public void processingInstruction(String target, String data) throws SAXException   
  34.     {   
  35.         System.out.println("<?"+target+" "+data+"?>");   
  36.     }   
  37.        
  38.     /**  
  39.      * 重新的方法  
  40.      */  
  41.     public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException   
  42.     {   
  43.         System.out.print("<"+qName);   
  44.         int len=attrs.getLength();   
  45.            
  46.         for(int i=0;i<len;i++)   
  47.         {   
  48.             System.out.print(" ");   
  49.             System.out.print(attrs.getQName(i));   
  50.             System.out.print("=\"");   
  51.             System.out.print(attrs.getValue(i));   
  52.             System.out.print("\"");   
  53.         }   
  54.         System.out.print(">");   
  55.     }   
  56.        
  57.     /**  
  58.      * 重新的方法  
  59.      */  
  60.     public void characters(char[] ch, int start, int length) throws SAXException   
  61.     {   
  62.         System.out.print(new String(ch,start,length));   
  63.     }   
  64.   
  65.     /**  
  66.      * 重新的方法  
  67.      */  
  68.     public void endElement(String uri, String localName, String qName) throws SAXException   
  69.     {   
  70.         System.out.print("</"+qName+">");   
  71.     }   
  72.        
  73.     public static void main(String arge[]){   
  74.         SAXParserFactory spf=SAXParserFactory.newInstance();   
  75.         try  
  76.         {   
  77.             SAXParser sp=spf.newSAXParser();   
  78.             sp.parse(new File("students.xml"),new SAXPrinter());   
  79.         }   
  80.         catch (ParserConfigurationException e)   
  81.         {   
  82.                
  83.             e.printStackTrace();   
  84.         }   
  85.         catch (SAXException e)   
  86.         {   
  87.                
  88.             e.printStackTrace();   
  89.         }   
  90.         catch (IOException e)   
  91.         {   
  92.             // TODO 自动生成 catch 块   
  93.             e.printStackTrace();   
  94.         }   
  95.     }   
  96.        
  97. }  
posted @ 2009-01-08 16:43  猪鼻驴耳  阅读(271)  评论(0)    收藏  举报