Java 使用SAX解析xml文件

程序来源:Java2 编程150例
下面的代码可以直接跑,希望能为初学使用SAX解析xml文件的朋友节省时间

package parseXML;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

//第一种方法直接解析
public class SAXDemo extends DefaultHandler{

    public static void main(String[] args) {
        try{

            SAXParser parser = 
                (SAXParser)Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
            SAXDemo handler = new SAXDemo();
            parser.setContentHandler(handler);
            parser.parse("parseXML/test.xml");
            

        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
    
    public void startElement(String uri,String localName,String qName,Attributes attr){
       System.out.println("Element的开始 uri: "+uri+"| localName: "+localName+"| qName: "+qName);
       if(attr.getLength()>0){
           for(int i = 0;i<attr.getLength();i++){
               System.out.println(attr.getQName(i)+" = "+attr.getValue(i));
           }
       }
    }
    
    public void endElement(String uri,String localName,String qName){
        System.out.println("Element的结束 uri: "+uri+"localName: "+localName+"qName: "+qName);
        System.out.println("=============================================================");
    }
    
}






package parseXML;

import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLFilterImpl;

//第二种方法:使用filter解析
public class SAXDemo2 extends DefaultHandler{

    public static void main(String[] args) {
        try{
            //生成SAXParser
            SAXParser parser = 
                (SAXParser)Class.forName("org.apache.xerces.parsers.SAXParser").newInstance();
            //生成Handler
            SAXDemo2 handler = new SAXDemo2();
        
            FilterHandler filter = new FilterHandler();

            //将刚才生成的handler和parser配置进filter
            filter.setParent(parser);
            filter.setContentHandler(handler);
            
            filter.parse("parseXML/test.xml");
        }catch(Exception e){
            System.out.println(e.toString());
        }
    }
    
    public void startElement(String uri,String localName,String qName,Attributes attr){
       System.out.println("Element的开始 uri: "+uri+"| localName: "+localName+"| qName: "+qName);
       if(attr.getLength()>0){
           for(int i = 0;i<attr.getLength();i++){
               System.out.println(attr.getQName(i)+" = "+attr.getValue(i));
           }
       }
    }
    
    public void endElement(String uri,String localName,String qName){
        System.out.println("Element的结束 uri: "+uri+"localName: "+localName+"qName: "+qName);
        System.out.println("=============================================================");
    }
    
    static class FilterHandler extends XMLFilterImpl{
        int index = 0;
        
        public void startElement(String uri,String localName,String qName,Attributes attr) throws SAXException{
            AttributesImpl attrImpl = new AttributesImpl(attr);
            for(int i = 0;i<attrImpl.getLength();i++){
                if(attrImpl.getQName(i).toUpperCase().equals("ID")){
                    attrImpl.removeAttribute(i);
                    break;
                }
            }
            attrImpl.addAttribute("","id","id","ID",String.valueOf(index++));
            super.startElement(uri,localName,qName,attrImpl);
        }
        
        public void endElement(String uri,String localName,String qName) throws SAXException{
            super.endElement(uri,localName,qName);
        }
        
        public void characters(char[] ch,int iStart,int iLength) throws SAXException{
            super.characters(ch,iStart,iLength);
        }
    }
}

你可能感兴趣的:(java,apache,编程,xml)