running Sax parser

心不动则不痛 提交于 2020-01-06 05:36:12

问题


Am new to using SAX parser .Can anyone tell me how to run it .and what all are required to run it (jdk )..Can i have a sax parser that can parse both android xml and a normal xml


回答1:


SAX parsers are implemented by creating a ContentHandler object which implements certain callback functions that correspond to events that happen while parsing an XML document. For example, the startDocument method is called when the parser begins parsing the document, and startElement is called when it discovers a new tag; similarly, endElement, endDocument, and error are called when the parser finds the end of a tag or document, or when an invalid sequence is discovered.

This example shows how to use a SAX parser. The key is that the MyHandler class extends the DefaultHandler class (which implements the ContentHandler interface) and overrides the empty implementations of each callback method.

Think of it this way: the Java SAXParser class knows how to parse XML documents but when it discovers things of interest it relies on some handler class to know what to do with them. The DefaultHandler class is a helper implementation which you can extend to pay attention to the interesting things.




回答2:


You could use a ContentHandler directly (see below) instead of extending the DefaultHandler if you want. I believe this level of SAX parsing is available on the Android platform.

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.XMLReader;

public class Demo {

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(new MyContentHandler());
        xr.parse("input.xml");
    }

    private static class MyContentHandler implements ContentHandler {

        public void setDocumentLocator(Locator locator) {
        }

        public void startDocument() throws SAXException {
        }

        public void endDocument() throws SAXException {
        }

        public void startPrefixMapping(String prefix, String uri)
                throws SAXException {
        }

        public void endPrefixMapping(String prefix) throws SAXException {
        }

        public void startElement(String uri, String localName, String qName,
            System.out.println("START " + qName);
        }

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

        public void characters(char[] ch, int start, int length)
                throws SAXException {
            System.out.println(new String(ch, start, length));
        }

        public void ignorableWhitespace(char[] ch, int start, int length)
                throws SAXException {
        }

        public void processingInstruction(String target, String data)
                throws SAXException {
        }

        public void skippedEntity(String name) throws SAXException {
        }

    }

}


来源:https://stackoverflow.com/questions/2718723/running-sax-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!