2011-01-11 76 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

我想,這應該工作。