【发布时间】:2010-12-31 00:10:21
【问题描述】:
我正在尝试通过 XML 解析数据并且它正在工作,但唯一的问题是它正在返回标记的最后一行。我正在使用歌词 Wiki 来测试一下
http://lyrics.wikia.com/LyricWiki:API
对于他们网站上带有艺术家蛋糕和歌曲一角钱的示例帖子,我得到的返回值是“当一个皱巴巴的 [...]”
我的代码如下所示:
公共类 ExampleHandler 扩展 DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private boolean in_outertag = false;
private boolean in_innertag = false;
private boolean in_mytag = false;
private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}
@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("LyricsResult")) {
this.in_outertag = true;
}else if (localName.equals("lyrics")) {
this.in_mytag = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("LyricsResult")) {
this.in_outertag = false;
}else if (localName.equals("lyrics")) {
this.in_mytag = false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
public void characters(char ch[], int start, int length) {
if(this.in_mytag){
myParsedExampleDataSet.setExtractedString(new String(ch, start, length));
}
} }
还有……
公共类 ParsedExampleDataSet { 私有字符串提取字符串 = null; private int extractInt = 0;
public String getExtractedString() {
return extractedString;
}
public void setExtractedString(String extractedString) {
this.extractedString = extractedString;
}
public int getExtractedInt() {
return extractedInt;
}
public void setExtractedInt(int extractedInt) {
this.extractedInt = extractedInt;
}
public String toString(){
return this.extractedString;
}
}
【问题讨论】: