简体   繁体   English

Android Java通过Web解析XML

[英]Android Java Parsing XML via Web

I'm trying to parse data via XML and it's working but the only problem is it is returning the last line of the tag. 我正在尝试通过XML解析数据,并且可以正常工作,但是唯一的问题是它返回了标签的最后一行。 I'm using Lyrics Wiki just to test out 我正在使用Lyrics Wiki进行测试

http://lyrics.wikia.com/LyricWiki:API http://lyrics.wikia.com/LyricWiki:API

For the example post on their site with artist cake and song dime the return value I get is "When a crumpled up [...]" 对于他们网站上带有艺术家蛋糕和一角钱的示例帖子,我得到的返回值是“当皱巴巴的时候。

My code looks as such: 我的代码如下所示:

public class ExampleHandler extends DefaultHandler{ 公共类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));
        }

} } }}

and... 和...

public class ParsedExampleDataSet { private String extractedString = null; 公共类ParsedExampleDataSet {私有字符串extractedString = null; private int extractedInt = 0; 私人诠释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;
}

} }

修复了将字符放入stringbuilder所需的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM