我在网上有一个类似这样的 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));
}
}
好的,问题来了……我怎样才能得到“我想要的以及上面的东西”,即使它包含在与“我已经知道如何得到的东西”类似的标签中?
提前致谢 :)
NB XML 文档就像一个预测,所以其他标签的日期和内容总是在变化的。