无法检索嵌套在另一个标记的内容中的XML标记

时间:2011-04-18 21:59:02

标签: android xml parsing sax

感谢阅读!

使用here中的XML解析教程作为参考,我试图用以下结构解析一个简单的XML RSS feed。

一切正常,所有值都会被解析,但以下情况除外:我无法获取<img>标记的内容。


<feed>
    <title>This is Title</title>
    <count>10</count>
    <desc>
        This is a description for a sample feed <img src="http://someimagelink.com/img.jpg" />
    </desc>
    <link>This is link</link>
</feed>

这就是endElement()方法的样子:


        @Override
        public void endElement(String uri, String localName, String qName)
            throws SAXException {
        if(localName.equals("feed")) {
            //Add Records object to ArrayList
            //Feed is a POJO class to store all the feed content. 
            //FeedList is an ArrayList to store multiple Feed objects.
            mFeedList.add(mFeed); 
        }
        else if(localName.equals("title")) {
            mFeed.setTitle(currentValue.toString());
        }
        else if(localName.equals("count")) {
            mFeed.setCount(currentValue.toString());
        }
        else if(localName.equals("desc")) {
            mFeed.setDesc(currentValue.toString());
        }
        else if(localName.equals("img")) {
             //NEVER hits here :(
            mFeed.setImageUrl(currentValue.toString());
        }
        else if(localName.equals("link")) {
            //BUT, hits here
            mFeed.setLink(currentValue.toString());
        }

由于<img>标记是<desc>标记的一部分,因此最后else if条件中的代码永远不会被执行。

注意:当我阅读<desc>标记时,我可以进行手动String搜索以检索<img>标记内容。但是,我确信必须有一种更有效的方式。

有人可以指导我获取<img>代码的内容吗?

谢谢!

编辑:更新了<img>标记。它现在正确关闭。

EDIT2:此处使用startElement()代码进行更新。还更新了Feed XML和startElement()代码。

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    if(localName.equals("feed")) {
        //Instantiate Feed object
        mFeed = new Feed();
    }
    else if(localName.equals("title")) {
            currentValue = new StringBuffer("");
            isBuffering = true;
    }
    else if(localName.equals("count")) {
            currentValue = new StringBuffer("");
            isBuffering = true;     
    }
    else if(localName.equals("desc")) {
        currentValue = new StringBuffer("");
        isBuffering = true;
    }
    else if(localName.equals("img")) {
            currentValue = new StringBuffer("");
            isBuffering = true;
        }
    }
    else if(localName.equals("link")) {
        currentValue = new StringBuffer("");
        isBuffering = true;
    }       
}

1 个答案:

答案 0 :(得分:1)

<img>标记实际上没有字符内容,并且您所追求的值必须从属性中拉出。

为此,您需要覆盖startElement(String namespaceURI, String localName, String qName, Attributes atts),或多或少地识别{​​{1}}标记,并从<img>参数中获取所需的值。

调试帮助:

使用这个(简单/愚蠢)处理程序:

atts
startElement: URI: '', localName: '', qName: 'feed'
Characters: '
    '
startElement: URI: '', localName: '', qName: 'title'
Characters: 'This is Title'
endElement:   URI: '', localName: '', qName: 'title'
Characters: '
    '
startElement: URI: '', localName: '', qName: 'count'
Characters: '10'
endElement:   URI: '', localName: '', qName: 'count'
Characters: '
    '
startElement: URI: '', localName: '', qName: 'desc'
Characters: '
        This is a description for a sample feed '
startElement: URI: '', localName: '', qName: 'img'
Attribute  URI: '', localName: 'src', qName: 'src', Value: 'http://someimagelink.com/img.jpg'
endElement:   URI: '', localName: '', qName: 'img'
Characters: '
    '
endElement:   URI: '', localName: '', qName: 'desc'
Characters: '
    '
startElement: URI: '', localName: '', qName: 'link'
Characters: 'This is link'
endElement:   URI: '', localName: '', qName: 'link'
Characters: '
'
endElement:   URI: '', localName: '', qName: 'feed'

这表明package com.donroby.so; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; public class DebugHandler extends DefaultHandler { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { printParseInfo("startElement:", uri, localName, qName); int attributesLength = attributes.getLength(); for (int i = 0; i < attributesLength; i++) { printAttributeInfo(attributes, i); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { printParseInfo("endElement: ", uri, localName, qName); } @Override public void characters(char[] chars, int start, int length) throws SAXException { String str = ""; for (int i = start; i < start + length; i++) str += chars[i]; System.out.println("Characters: '" + str + "'"); } private void printAttributeInfo(Attributes attributes, int i) { System.out.println(String.format("%s URI: '%s', localName: '%s', qName: '%s', Value: '%s'", "Attribute ", attributes.getURI(i), attributes.getLocalName(i), attributes.getQName(i), attributes.getValue(i))); } private void printParseInfo(String type, String uri, String localName, String qName) { System.out.println(String.format("%s URI: '%s', localName: '%s', qName: '%s'", type, uri, localName, qName)); } } 标记确实生成了开始和结束事件。