zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    如何用js刷新aspxgridviw
    ASPxSpinEdit 控件的三元判断
    关于cookie
    asp.net解决数据转换为DBNULL的问题
    Devexpress 中如何写ASPxGridView新增修改时的数据验证
    ASPxGridView中批量提交及个别提交的写法
    c#中如何做日期的三元判断(日期不为空赋值)
    c#中如何不通过后台直接用js筛选gridview中的数据条件筛选查询?
    devexpress中如何绑定ASPxTreeList控件
    如何在后台动态生成ASPxCheckBoxList标签并循环(数据调用存储过程)
  • 原文地址:https://www.cnblogs.com/cici20166/p/6380375.html
Copyright © 2011-2022 走看看