2011-01-11 86 views
1

hii大家好,sowmya是全新的android系统请帮助我,解析下面的本地xml文件,但只得到第一个问题的输出(第一组标签),但第二个问题(重复标签)我没有得到,, thanx提前在android中解析本地XML文件

<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使用这个作为xml处理程序文件 ////////////////////////////////////// ////////////////////////////////////////////////// //// package com.itwine;

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

公共类XMLHandler扩展了DefaultHandler {

// =========================================================== 
// 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)); 
    } 
} 

}

+1

解析的主要思想是提取一些标签之间的数据,这些标签通常重复多次。此外,您拥有本地或远程xml数据源的事实不需要不同的分析器。您的问题似乎是您通过解析检索数据后存储数据的方式。 – Adinia 2011-01-11 15:06:45

回答

2

我已经做了XML解析的机器人。检查此链接为构建..

compliantbox.com/XmlParsingcompliant.zip

只是与本地地址替换URL ...................

+0

thanx for ur reply,但该网址中的数据包含垃圾数据我应该在本地文件中提供哪些数据,,,,我在语法上有一点混淆,生病请给我解析包含重复性的本地xml的代码标签(我的意思是同一个标签多次使用) – Sowmya 2011-01-11 13:39:22

+0

thak u非常适合提供这个zip文件,它帮助了我很多 – Sowmya 2011-01-12 04:08:21

0

实施在你的代码中是这样的,如果条件为真,它会在第一个实例出来,所以你的代码不能看到第二个mytag。

如果允许改变,可以很好地改变标签名称:
mytag11
mytag22

我想,这应该工作。