我有一個XML文件,網上類似這樣的:如何解析第二個XML標籤?
<example>
<date>2012-10-13</date>
<bob>What I already know how to get</bob>
</example>
<example>
<date>2012-10-14</date>
<bob>What I want as well as the above</bob>
</example>
下面是我用得到的「我已經知道如何得到」標籤的數據內容:
/**
* 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("example")) {
this.in_example = true;
} else if (localName.equals("bob")) {
this.in_bob = true;
}
}
/**
* Gets be called on closing tags like: </tag>
*/
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("example")) {
this.in_example = false;
} else if (localName.equals("bob")) {
this.in_bob = false;
}
}
/**
* Gets be called on the following structure: <tag>characters</tag>
*/
@Override
public void characters(char ch[], int start, int length) {
if (this.in_bob) {
// A custom DataParser
myDataParser.setExtractedString(new String(ch, start, length));
}
}
好,所以這裏有個問題......即使它包含了類似於「我已經知道如何獲得的東西」的標籤,我怎樣才能得到「我想要的和上面一樣」?
在此先感謝:)
N.B. XML文檔就像預測一樣,所以其他標籤的日期和內容將始終在變化。
你試過用dom解析器嗎? – Raghunandan