Java document operation - SAX parsing

Author: San Nian

Java document operation - SAX parsing


1. Brief explanation of related concepts

[Serialization] The process of converting the state information of an object into a form that can be stored or transmitted

  • Serialization: The process of converting objects into binary information
  • Deserialization: The process of converting binary information back to an object

    The purpose of serialization:

    1. Make custom objects permanent in some form
    2. transfer objects from one place to another

【XML】Extensible Markup Language

The role
of XML After the data format is converted into XML format, cross-platform data sharing is truly realized.
In different languages, the way of parsing XML is the same

Advantages and disadvantages of SAX parsing
Advantages: small memory footprint
Disadvantages : read-only forward, read once

XML Grammar Rules
1. All elements must have closing tags2
. Case sensitive3
. Must be properly nested4
. Must have root
element5. Attribute values ​​must be quoted6
. Spaces are preserved

xml file example:

<?xml version="1.0" encoding="utf-8"?>
<fruits>
  <fruit id="1">
    <name>苹果</name>
    <price>3.0</price>
    <count>51</count>
  </fruit>
  <fruit id="2">
    <name>香蕉</name>
    <price>3.5</price>
    <count>17</count>
  </fruit>
  <fruit id="3">
    <name>生梨</name>
    <price>2.5</price>
    <count>15</count>
  </fruit>
  <fruit id="4">
    <name>桃子</name>
    <price>5.0</price>
    <count>10</count>
  </fruit>
  <fruit id="5">
    <name>西瓜</name>
    <price>10.0</price>
    <count>20</count>
  </fruit>
</fruits>

2. Code example

There are two ways to analyze this article. The first chapter is to use the relevant jar package to operate, and the second is to use the java native code to realize (reflection)
about the download of the jar package, please Baidu


import java.io.File;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
 * SAX解析(jar包) 方式一
 * @author yujie
 *
 */
public class SN_9IO_SAX_JAR {
    public static void main(String[] args) {
        parse();
    }
    public static void parse() {

        //1.获取SAXReader对象
        SAXReader saxReader = new SAXReader();
        //2.获取需要解析的文件
        File file = new File("person.xml");
        //3.解析文件获取Document对象
        Document read =null;
        try {
            read = saxReader.read(file);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //4.解析文件获取文件中的根节点
        Element rootElement = read.getRootElement();
        //获取所有的节点属性
        List<Element> elements = rootElement.elements();
        for (Element element:elements) {
            System.out.println(element.attributeValue("name"));
        }
    }
}


import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
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;

/**
 * SAX解析(反射) 方式二(繁琐,不需要导入 jar包 提供参考)
 * @author yujie
 *
 */
public class SN_9IO_SAX {
    private static ArrayList<Person> list;
    public static void main(String[] args) {
        parse();
    }

    /**
     * SAXParserFactory ——> SAXParser
     */
    public static void parse() {
        try {
            FileInputStream fis = new FileInputStream("person.xml");

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();

            //解析
            parser.parse(fis, new MyHandler());
            //打印
            for(Person p:list) {
                System.out.println(p);
            }

        } catch (ParserConfigurationException | SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class MyHandler extends DefaultHandler {
        private static Person person;
        private static String upTag;//上一次读到的标签

        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
            list = new ArrayList<>();
        }

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            super.startElement(uri, localName, qName, attributes);
            if("person".equals(qName)) {
                person = new Person();
                person.setName(attributes.getValue(0));
            }else {
                upTag = qName;
            }
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);
            String c = new String(ch, start, length);
            switch (upTag) {
            case "age":
                person.setAge(Integer.parseInt(c));
                break;
            case "job":
                person.setJob(c);
                break;
            default:
                break;
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(uri, localName, qName);
            if("person".equals(qName)) {
                list.add(person);
                person = null;
            }
            upTag = null;
        }

        @Override
        public void endDocument() throws SAXException {
            super.endDocument();
            list.trimToSize();
            System.out.println("解析完成");
        }
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326699051&siteId=291194637