SAX解析XML-例子

Posted ~~Cc

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于SAX解析XML-例子的主要内容,如果未能解决你的问题,请参考以下文章

Android 之XML数据解析—— SAX解析

在BlackBerry中使用SAX解析器解析XML

XML解析之SAX解析技术案例

Sax 解析 XMl

Java XML解析

javaweb学习总结十二(JAXP对XML文档进行SAX解析)