1. 程式人生 > >XML —— SAX方式解析XML

XML —— SAX方式解析XML

1. SAX 方式解析 XML 原理

  • SAX 的工作原理簡單地說就是對文件進行順序掃描,當掃描到文件(document)開始與結束、元素(element) 開始與結束等地方時通知事件處理函式,由事件處理函式做相應動作,然後繼續同 樣的掃描,直至文件結束。
  • 優點:相對於DOM方式消耗資源比較少,適合大檔案解析;
  • 缺點:只能讀取不能修改;開發複雜;

 

2. SAX 方式解析 XML 示例

  1. 掃描文件

    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 SAX01 extends DefaultHandler{ @Override public void startDocument() throws SAXException { //開始掃描文件事件 System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); } @Override
    public void endDocument() throws SAXException { //結束掃描文件事件 System.out.print("\n 掃描文件結束"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { //開始掃描元素事件 uri字首 System.out.print("<"); System.out.print(qName);
    if(attributes!=null){ for(int i=0;i<attributes.getLength();i++){ System.out.print(" "+attributes.getQName(i)+"=\""+attributes.getValue(i)+"\""); } } System.out.print(">"); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { //結束掃描元素事件 System.out.print("</"); System.out.print(qName); System.out.print(">"); } @Override public void characters(char[] ch, int start, int length) throws SAXException { // 掃描文字節點事件 System.out.print(new String(ch,start,length)); } public static void main(String[] args) throws Exception{ SAXParserFactory factory=SAXParserFactory.newInstance(); SAXParser parser=factory.newSAXParser(); parser.parse("src/student.xml", new SAX01()); } }

     

  2.  獲取值

    import java.util.ArrayList;
    import java.util.List;
    
    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;
    
    import com.learn.model.Student;
    
    public class SAX02 extends DefaultHandler{
        private List<Student> students=null;
        private Student student=null;
        private String preTag=null; // 標記上一個節點名稱
        
        @Override
        public void startDocument() throws SAXException {
            System.out.println("開始讀取學生資訊");
            students=new ArrayList<Student>();
        }
    
        @Override
        public void endDocument() throws SAXException {
            System.out.println("\n 學生資訊讀取完畢");
        }
    
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {
            if("student".equals(qName)){
                student=new Student();
                student.setId(attributes.getValue(0));
            }
            preTag=qName;
        }
    
        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {
            if("student".equals(qName)){
                students.add(student);
                student=null;
            }
            preTag=null;
        }
    
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {
            if(preTag!=null){
                String content=new String(ch,start,length);
                if("name".equals(preTag)){
                    student.setName(content);
                }else if("sex".equals(preTag)){
                    student.setSex(content);
                }else if("age".equals(preTag)){
                    student.setAge(Integer.parseInt(content));
                }
            }
        }
    
        public static void main(String[] args) throws Exception{
            SAXParserFactory factory=SAXParserFactory.newInstance();
            SAXParser parser=factory.newSAXParser();
            SAX02 sax02=new SAX02();
            parser.parse("src/students.xml", sax02);
            for(Student s:sax02.students){
                System.out.println(s);
            }
        }
    }