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

| 谢谢阅读! 使用此处的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>
标签的一部分,因此最后never6ѭ条件下的代码将永远不会执行。 注意:读取
<desc>
标记时,可以进行手动
String
搜索以检索
<img>
标记内容。但是,我相信必须有一种更有效的方法。 有人可以指导我获取
<img>
标签的内容吗? 谢谢! 编辑:更新了“ 0”标签。现在已正确关闭。 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;
    }       
}
已邀请:
<img>
标记实际上没有字符内容,必须将您想要的值从属性中拉出。 为此,您需要覆盖
startElement(String namespaceURI, String localName, String qName, Attributes atts)
,在执行操作时或多或少地识别
<img>
标记,并从
atts
参数中获取所需的值。 调试帮助: 使用此(简单/愚蠢)处理程序:
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));
    }

}
startElement:URI:\'\',localName:\'\',qName:\'feed \' 字符:\' \' startElement:URI:\'\',localName:\'\',qName:\'title \' 字符:\'这是标题\' endElement:URI:\'\',localName:\'\',qName:\'title \' 字符:\' \' startElement:URI:\'\',localName:\'\',qName:\'count \' 字符:\'10 \' endElement:URI:\'\',localName:\'\',qName:\'count \' 字符:\' \' startElement:URI:\'\',localName:\'\',qName:\'desc \' 字符:\' 这是样本Feed的描述, startElement:URI:\'\',localName:\'\',qName:\'img \' 属性URI:\'\',localName:\'src \',qName:\'src \',值:\'http://someimagelink.com/img.jpg \' endElement:URI:\'\',localName:\'\',qName:\'img \' 字符:\' \' endElement:URI:\'\',localName:\'\',qName:\'desc \' 字符:\' \' startElement:URI:\'\',localName:\'\',qName:\'link \' 字符:\'这是链接\' endElement:URI:\'\',localName:\'\',qName:\'link \' 字符:\' \' endElement:URI:\'\',localName:\'\',qName:\'feed \' 这表明“ 0”标记确实会生成开始和结束事件。

要回复问题请先登录注册