【问题标题】:Android Java Parsing XML via WebAndroid Java 通过 Web 解析 XML
【发布时间】: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;
}

}

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    修复了只需要将字符放入字符串生成器

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多