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));
}
}
}
解析的主要思想是提取一些標籤之間的數據,這些標籤通常重複多次。此外,您擁有本地或遠程xml數據源的事實不需要不同的分析器。您的問題似乎是您通過解析檢索數據後存儲數據的方式。 – Adinia 2011-01-11 15:06:45