运行Sax解析器 [英] running Sax parser

查看:141
本文介绍了运行Sax解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的使用SAX解析器。任何人都告诉我如何运行它和所有需要运行它(jdk)..我可以有一个sax解析器,可以解析android xml和一个正常的XML


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

推荐答案

您可以直接使用ContentHandler(见下文),而不是扩展DefaultHandler。我相信这个级别的SAX解析在Android平台上可用。

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 {
        }

    }

}

这篇关于运行Sax解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆