android开发之读取xml文件

Android读取xml文件:

1.SAX

1.生成xml读取器SAXParser

2.创建自己的内容处理器,处理xml节点、属性等等,需要继承于 DefaultHandler 并且has a自己的数据结构

3.读取器1加载处理器2,读取xml

/* 创建xml文件的URL*/
               URL url = new URL("http://www.linuxidc.com/example.xml");

/* 从SAXParserFactory获取SAXParser. */
               SAXParserFactory spf = SAXParserFactory.newInstance();
               SAXParser sp = spf.newSAXParser();

/* 从SAXParser获取XMLReader. */
               XMLReader xr = sp.getXMLReader();
               /* 创建我们自己的内容处理器*/
               ExampleHandler myExampleHandler = new ExampleHandler();
               /* 用内容处理器处理XMLReader*/
               xr.setContentHandler(myExampleHandler);
              
               /* XMLReader获取XML文件 */
               xr.parse(new InputSource(url.openStream()));
              

/* 用我们的ExampleHandler解析XML中的数据. */
               ParsedExampleDataSet parsedExampleDataSet =
                  myExampleHandler.getParsedData();

自己的处理器的数据结构:
     private ParsedExampleDataSet myParsedExampleDataSet =
                  new ParsedExampleDataSet();
 
     public ParsedExampleDataSet getParsedData() {
          return this.myParsedExampleDataSet;
     }
     @Override
     public void startDocument() throws SAXException {
          this.myParsedExampleDataSet = new ParsedExampleDataSet();
          Log.v("ParsingXML", "startDocument()");
     }
     @Override
     public void endDocument() throws SAXException {
          Log.v("ParsingXML", "endDocument()");
     }
     /** 当遇到开始标签时被调用,比如:
      * <tag>
      * 可以得到标签属性值,比如:
      * <tag attribute="attributeValue">*/
     @Override
     public void startElement(String namespaceURI, String localName,
               String qName, Attributes atts) throws SAXException {
          if (localName.equals("outertag")) {
               this.in_outertag = true;
          }else if (localName.equals("innertag")) {
               this.in_innertag = true;
          }else if (localName.equals("mytag")) {
               this.in_mytag = true;
          }else if (localName.equals("tagwithnumber")) {
               // 提取标签属性值
               String attrValue = atts.getValue("thenumber");
               int i = Integer.parseInt(attrValue);
               myParsedExampleDataSet.setExtractedInt(i);
          }
          Log.v("ParsingXML", "StartElement(): "+ localName);
     }
    
     /** 当遇到终止标签时被调用,比如:
      * </tag> */
     @Override
     public void endElement(String namespaceURI, String localName, String qName)
               throws SAXException {
          if (localName.equals("outertag")) {
               this.in_outertag = false;
          }else if (localName.equals("innertag")) {
               this.in_innertag = false;
          }else if (localName.equals("mytag")) {
               this.in_mytag = false;
          }else if (localName.equals("tagwithnumber")) {
               // Nothing to do here
          }
          Log.v("ParsingXML", "endElement(): "+ localName);
     }
    
     /** 当遇到如下结构时被调用
      * <tag>characters</tag>
      */
     @Override
    public void characters(char ch[], int start, int length) {
          if(this.in_mytag){
          String str = new String(ch, start, length);
          myParsedExampleDataSet.setExtractedString(str);
          Log.v("ParsingXML", "characters(): " + str);
     }
     
    }

2.DOM

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwygdg.html