SAX解析器

1、继承类DefaultHandler

package com.SSLSocket.test;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class TestDefaultHandler extends DefaultHandler {

    public void endDocument() throws SAXException {
        System.out.println("endDocument");
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println("endElement"+"//"+uri+"//"+localName+"//"+qName);
    }

    public void startDocument() throws SAXException {
        System.out.println("startDocument");
    }

    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println("startElement+"+"//"+uri+"//"+localName+"//"+qName+"//"+attributes.getIndex(qName));
    }

}

2、使用

SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser(); 
                        String ss = "D:/02.WorkSpace/01.Eclipse/test/src/com/SSLSocket/test/dom.xml";
            InputStream is = new FileInputStream(ss);
            TestDefaultHandler handle = new TestDefaultHandler();
            saxParser.parse(is, handle);
            is.close();
原文地址:https://www.cnblogs.com/wbjgogogo/p/5193399.html