如何解决无法检索嵌套在另一个标签内容内的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 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,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 qName) throws SAXException {
printParseInfo(\"endElement: \",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 qName) {
System.out.println(String.format(\"%s URI: \'%s\',qName: \'%s\'\",type,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”标记确实会生成开始和结束事件。版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。