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);
            }
        }
    }

猜你喜欢

转载自www.cnblogs.com/Cocoomg/p/9901615.html