Parsing local XML file using Sax in Android

后端 未结 3 1824
遥遥无期
遥遥无期 2020-12-01 10:01

Can anyone tell me how to parse a local XML file stored in the system using SAX, with an example code? Please also tell me where can I find information on that.

3条回答
  •  鱼传尺愫
    2020-12-01 10:38

    sample code

    1. Create documentBuilderFactory

      DocumentBuilderFactory factory= DocumentBuilderFactory.newInstance();

      1. Create DocumentBuilder

      DocumentBuilder builder=factory. newDocumentBuilder();

      1. get input stream ClassLoader cls=DomReader.class.getClassLoader(); InputStream is=cls.getResourceAsStream("xml file");
        1. parse xml file and get Document object by calling parse method on DocumentBuilder object. Document document=builder.parse(is);
        2. Traverse dom tree using document object. SAX: Simple xml parsing. It parses node by node Traversing is from top to bottom Low memory usage Back navigation is not possible with sax.

      //implementing required handlers public class SaxParse extends DefaultHandler{ } //new instance of saxParserFactory SAXParserFactory factory=SAXParserFactory.newInstance(); //NEW INSTANCE OF SAX PARSER SAXParser saxparser=factory.newSAXParser(); //Parsing xml document SAXParser.parse(new File(file to be parsed), new SAXXMLParserImpl());

提交回复
热议问题