首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在android中解析本地XML文件

在android中解析本地XML文件
EN

Stack Overflow用户
提问于 2011-01-11 20:52:21
回答 2查看 1.2K关注 0票数 1

大家好,我是sowmya,我是android的新手,请任何人能帮助我,我正在解析以下本地xml文件,但我只得到了第一个问题(第一组标签)的输出,但第二个问题(重复的标签)我没有得到,,谢谢提前

代码语言:javascript
复制
<innertag sampleattribute="innertagAttribute">
<mytag>1)  How did u find the hotel house keeping overall in the year  </mytag>
代码语言:javascript
复制
    <innertag sampleattribute="innertagAttribute">
<mytag>2)  How do u rate the hotel house keeping overall in the year  </mytag>
代码语言:javascript
复制
</outertag>

////////////////////////////////////////////////////////////////////////////////////////////正在将其用作xml处理程序文件////////////////////////////////////////////////////////////////////////////////////////包com.itwine;

导入org.xml.sax.Attributes;导入org.xml.sax.SAXException;导入org.xml.sax.helpers.DefaultHandler;

公共类XMLHandler扩展了DefaultHandler{

代码语言:javascript
复制
 // ===========================================================
 // Fields
 // ===========================================================

 private boolean in_outertag = false;
 private boolean in_innertag = false;
 private boolean in_mytag = false;




 private boolean in_innertag1 = false;
 private boolean in_mytag1= false;




 private boolean in_innertag2 = false;
 private boolean in_mytag2 = false;

 private XMLDataSet myParsedExampleDataSet = new XMLDataSet();
 private XMLDataSet myParsedExampleDataSet1 = new XMLDataSet();
 private XMLDataSet myParsedExampleDataSet2 = new XMLDataSet();
 private XMLDataSet myParsedExampleDataSet3 = new XMLDataSet();


 // ===========================================================
 // Getter & Setter
 // ===========================================================

 public XMLDataSet getParsedData() {
      return this.myParsedExampleDataSet;
 }

 public XMLDataSet getParsedData1() {
     return this.myParsedExampleDataSet1;
}
 public XMLDataSet getParsedData2() {
     return this.myParsedExampleDataSet2;
}
 public XMLDataSet getParsedData3() {
     return this.myParsedExampleDataSet3;
}


 // ===========================================================
 // Methods
 // ===========================================================
 @Override
 public void startDocument() throws SAXException {
      this.myParsedExampleDataSet = new XMLDataSet();

      this.myParsedExampleDataSet1 = new XMLDataSet();

      this.myParsedExampleDataSet2 = new XMLDataSet();

      this.myParsedExampleDataSet3 = new XMLDataSet();
 }

 @Override
 public void endDocument() throws SAXException {
      // Nothing to do
 }

 /** Gets be called on opening tags like:
  * <tag>
  * Can provide attribute(s), when xml was like:
  * <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")) 
      {
           // Extract an Attribute
           String attrValue = atts.getValue("thenumber");
           int i = Integer.parseInt(attrValue);
           myParsedExampleDataSet.setExtractedInt(i);
      }

      else if (localName.equals("innertag1"))
      {
           this.in_innertag1 = true;
      }

      else if (localName.equals("mytag1")) 
      {
           this.in_mytag1 = true;
      }

      else if (localName.equals("tagwithnumber1")) 
      {
           // Extract an Attribute
           String attrValue = atts.getValue("thenumber");
           int j = Integer.parseInt(attrValue);
           myParsedExampleDataSet1.setExtractedInt(j);
      }

      else if (localName.equals("innertag2"))
      {
           this.in_innertag2 = true;
      }

      else if (localName.equals("mytag2")) 
      {
           this.in_mytag2 = true;
      }

      else if (localName.equals("tagwithnumber2")) 
      {
           // Extract an Attribute
           String attrValue = atts.getValue("thenumber");
           int k = Integer.parseInt(attrValue);
           myParsedExampleDataSet2.setExtractedInt(k);
      }

      else if (localName.equals("QIA")) 
      {
           // Extract an Attribute



      }


 }


 /** Gets be called on closing tags like:
  * </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
      }


      else if (localName.equals("innertag1")) 

      {
           this.in_innertag1 = false;
      }

      else if (localName.equals("mytag1")) 

      {
           this.in_mytag1 = false;
      }

      else if (localName.equals("tagwithnumber1")) {
           // Nothing to do here
      }

      else if (localName.equals("innertag2")) 

      {
           this.in_innertag2 = false;
      }

      else if (localName.equals("mytag2")) 

      {
           this.in_mytag2 = false;
      }

      else if (localName.equals("tagwithnumber2")) {
           // Nothing to do here
      }
 }


 /** Gets be called on the following structure:
  * <tag>characters</tag> */
 @Override
public void characters(char ch[], int start, int length) {
      if(this.in_mytag){
      myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
 }

      else if(this.in_mytag1){
          myParsedExampleDataSet1.setExtractedString(new String(ch, start, length));
     }

      else if(this.in_mytag2){
          myParsedExampleDataSet2.setExtractedString(new String(ch, start, length));
     }
}

}

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-01-11 21:23:18

我已经在android中完成了xml解析。检查此链接以获取内部版本..

compliantbox.com/XmlParsingcompliant.zip

只需将该url替换为您的本地地址...................

票数 2
EN

Stack Overflow用户

发布于 2011-01-11 21:38:31

你代码中的实现是这样的,如果条件为真,它在第一个instance..so出现,你的代码看不到第二个mytag。

如果允许更改,您可以很好地更改标记名称:

mytag11

mytag22

我想,这应该行得通。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4657805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档