• SAX解析XML-例子


    1.要解析的xml

    <?xml version="1.0" encoding="UTF-8"?>
    <employees>
        <employee id="001">
            <name>cici</name>
            <department>finace</department>
            <supervisor>lily</supervisor>
        </employee>
        <employee id="002">
            <name>alex</name>
            <department>develope</department>
            <supervisor>lily</supervisor>
        </employee>
    </employees>

    2.继承DefaultHandler的子类EmployeeHandler.java,重写方法

    package sax;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXNotRecognizedException;
    import org.xml.sax.SAXNotSupportedException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;
    
    public class SaxXMLTest {
        public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException{
            readXMLBySaxParser();
            readXMLByXMLReader();
        }
    
        private static void readXMLBySaxParser() throws ParserConfigurationException,
                SAXException, IOException {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            EmployeeHandler handler = new EmployeeHandler("employee");
            parser.parse("src\sax\employees.xml", handler);
            List<Map<String, String>> employees = handler.getEmployees();
            System.out.println(employees.toString());
        }
    
        private static void readXMLByXMLReader() throws SAXException,
                SAXNotRecognizedException, SAXNotSupportedException, FileNotFoundException, IOException {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            //打开解析器验证的功能
            reader.setFeature("http://xml.org/sax/features/validation",true);
            //开启明明空间特性
            reader.setFeature("http://xml.org/sax/features/namespaces",true); 
            EmployeeHandler handler = new EmployeeHandler("employee"); 
            reader.setContentHandler(handler);
            reader.parse(new InputSource(new BufferedInputStream(new FileInputStream("src\sax\employees.xml"))));
        }
    }
    View Code

    3.测试类 SaxXMLTest.java,用SAXParser和XMLReader两种方式解析

    package sax;
    
    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;
    
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXNotRecognizedException;
    import org.xml.sax.SAXNotSupportedException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.XMLReaderFactory;
    
    public class SaxXMLTest {
        public static void main(String[] args) throws SAXException, ParserConfigurationException, IOException{
            readXMLByHandler();
            readXMLByXMLReader();
        }
    
        private static void readXMLByHandler() throws ParserConfigurationException,
                SAXException, IOException {
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            
            EmployeeHandler handler = new EmployeeHandler("employee");
            parser.parse("src\sax\employees.xml", handler);
            List<Map<String, String>> employees = handler.getEmployees();
            System.out.println(employees.toString());
        }
    
        private static void readXMLByXMLReader() throws SAXException,
                SAXNotRecognizedException, SAXNotSupportedException, FileNotFoundException, IOException {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            //打开解析器验证的功能
            reader.setFeature("http://xml.org/sax/features/validation",true);
            //开启明明空间特性
            reader.setFeature("http://xml.org/sax/features/namespaces",true); 
            EmployeeHandler handler = new EmployeeHandler("employee"); 
            reader.setContentHandler(handler);
            reader.parse(new InputSource(new BufferedInputStream(new FileInputStream("src\sax\employees.xml"))));
        }
    }
    View Code
  • 相关阅读:
    Invalid character found in the request target.The valid characters are defined in RFC 7230 and RFC3986
    Calendar的用法
    spring boot+mybatis+mysql增删改查分页
    HIVE-利用ow_number() OVER(PARTITION BY)函数介绍求TOP-K
    Sqoop-从hive导出分区表到MySQL
    Sqoop--Free-form Query Imports 自由查询模式下$CONDITIONS关键字的作用
    HIVE-执行hive的几种方式,和把HIVE保存到本地的几种方式
    HIVE-分桶表的详解和创建实例
    HIVE-几道经典的hive题目
    HIVE-如何查看执行日志
  • 原文地址:https://www.cnblogs.com/cici20166/p/6380375.html
Copyright © 2020-2023  润新知