开发者

Parsing a local XML file in android

开发者 https://www.devze.com 2023-02-03 09:43 出处:网络
hii everyone am sowmya am brand new to android please can any one help me, am parsing following local xml file ,but am only getting output for 1st question(first set of tags) but 2nd question(repeated

hii everyone am sowmya am brand new to android please can any one help me, am parsing following local xml file ,but am only getting output for 1st question(first set of tags) but 2nd question(repeated tags) am not getting ,,thanx in advance

<innertag sampleattribute="innertagAttribute">
<mytag>1)  How did u find the hotel house keeping overall in the year  </mytag>

    <innertag sampleattribute="innertagAttribute">
<mytag>2)  How do u rate the hotel house keeping overall in the year  </mytag>

</outertag>

//////////////////////////////////////////////////////////////////////////////////////////// am using this as xml handler file //////////////////////////////////////////////////////////////////////////////////////////// package com.itwine;

import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler;

public class XMLHandler extends DefaultHandler{

 // ===========================================================
 // Fields
 // ===========================================================

 private boolean in_开发者_如何学Gooutertag = 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));
     }
}

}


i've done the xml parsing in android. check this link for the build..

compliantbox.com/XmlParsingcompliant.zip

just replace the url with your local address...................


The implementation in your code is such that, if condition is true it comes out at the first instance..so your code cannot see the second mytag.

If are allowed to change, you can very well change the tag names:
mytag11
mytag22

I think, this should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号