无法检索嵌套在另一个标记的内容的XML标签 [英] Not able to retrieve XML tag nested within content of another tag

查看:340
本文介绍了无法检索嵌套在另一个标记的内容的XML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您阅读!

使用XML解析教程从这里作为参考,我想解析具有以下结构的简单XML的RSS提要。

一切正常,所有值都分析,除了以下的情况:我不能够得到的内容< IMG> 标签

 
<饲料>
    <标题>这是标题< /标题>
    <计数大于10< /计数>
    <&说明GT;
        这是一个样本供给℃的描述; IMG SRC =htt​​p://someimagelink.com/img.jpg/>
    < /递减>
    <链接>这是链接< /链接>
< /饲料>

这是的endElement()方法看起来是这样的:

 
        @覆盖
        公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
            抛出的SAXException {
        如果(localName.equals(饲料)){
            //添加记录反对ArrayList的
            //饲料是一个POJO类来存储所有的提要内容。
            // FeedList是存储多个频道对象的ArrayList。
            mFeedList.add(mFeed);
        }
        否则,如果(localName.equals(标题)){
            mFeed.setTitle(currentValue.toString());
        }
        否则,如果(localName.equals(计数)){
            mFeed.setCount(currentValue.toString());
        }
        否则,如果(localName.equals(DESC)){
            mFeed.setDesc(currentValue.toString());
        }
        否则如果(localName.equals(IMG)){
             //嗟这里:(
            mFeed.setImageUrl(currentValue.toString());
        }
        否则,如果(localName.equals(链接)){
            //但是这里打
            mFeed.setLink(currentValue.toString());
        }

由于< IMG> 标签的一部分<说明> 标记中,code在最后否则,如果条件永远不会被执行。

注意::当我读了<说明> 标签,我可以做一个人工字符串搜索检索< IMG> 标记的内容。但是,我相信必须有一个更有效的方式。

有人可以指导我得到的内容< IMG> 标签

谢谢!

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

EDIT2:更新的startElement() code在这里。还更新了饲料XML和的startElement() code。

  @覆盖
公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
        属性属性)抛出的SAXException {    如果(localName.equals(饲料)){
        //实例化对象饲料
        mFeed =新饲料();
    }
    否则,如果(localName.equals(标题)){
            CurrentValue的=新的StringBuffer();
            isBuffering = TRUE;
    }
    否则,如果(localName.equals(计数)){
            CurrentValue的=新的StringBuffer();
            isBuffering = TRUE;
    }
    否则,如果(localName.equals(DESC)){
        CurrentValue的=新的StringBuffer();
        isBuffering = TRUE;
    }
    否则如果(localName.equals(IMG)){
            CurrentValue的=新的StringBuffer();
            isBuffering = TRUE;
        }
    }
    否则,如果(localName.equals(链接)){
        CurrentValue的=新的StringBuffer();
        isBuffering = TRUE;
    }
}


< IMG> 标记实际上已经没有字符的内容,你后的价值具有被拉出的属性。

要做到这一点,你需要重写的startElement(字符串的namespaceURI,字符串的localName,字符串QNAME,属性的ATT),识别< IMG&GT ; 标签或多或少你正在做的,并获得您需要出的ATT值参数

调试帮助:

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

 包com.donroby.so;进口org.xml.sax.Attributes;
进口org.xml.sax.SAXException;
进口org.xml.sax.helpers.DefaultHandler中;公共类DebugHandler扩展的DefaultHandler {    @覆盖
    公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,属性的属性)抛出的SAXException {
        printParseInfo(的startElement,URI的localName,QNAME);
        INT attributesLength = attributes.getLength();
        的for(int i = 0; I< attributesLength;我++){
            printAttributeInfo(属性,I);
        }
    }    @覆盖
    公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)抛出的SAXException {
        printParseInfo(的endElement:URI,localName创建,QNAME);
    }    @覆盖
    公共无效字符(字符[]字符,诠释开始,诠释长度)抛出的SAXException {
        字符串str =;
        的for(int i =启动; I<启动+长度;我+ +)
          STR + =字符[I]        的System.out.println(字符:'+ STR +');
    }    私人无效printAttributeInfo(属性属性,int i)以{
        的System.out.println(的String.format(%s的URI:'%s'的,的localName:'%s'的,QNAME:'%s'的,值:'%s'的,属性,
                attributes.getURI(i)中,attributes.getLocalName(ⅰ),attributes.getQName(ⅰ),attributes.getValue(ⅰ)));
    }    私人无效printParseInfo(字符串类型,URI字符串,字符串的localName,字符串QNAME){
        的System.out.println(的String.format(%s的URI:'%s'的,的localName:'%s'的,QNAME:'%s'的,类型,URI的localName,QNAME));
    }}


的startElement:URI:'',的localName:'',QNAME:喂
性状:
    
的startElement:URI:'',的localName:'',QNAME:'标题'
性状:这是标题
的endElement:URI:'',的localName:'',QNAME:'标题'
性状:
    
的startElement:URI:'',的localName:'',QNAME:计数
性状:'10'
的endElement:URI:'',的localName:'',QNAME:计数
性状:
    
的startElement:URI:'',的localName:'',QNAME:递减
性状:
        这是一个饲料样品的描述
的startElement:URI:'',的localName:'',QNAME:IMG
属性URI:'',的localName:'src'中,QNAME:'src'中,值:'http://someimagelink.com/img.jpg
的endElement:URI:'',的localName:'',QNAME:IMG
性状:
    
的endElement:URI:'',的localName:'',QNAME:递减
性状:
    
的startElement:URI:'',的localName:'',QNAME:链接
性状:这是链接
的endElement:URI:'',的localName:'',QNAME:链接
性状:

的endElement:URI:'',的localName:'',QNAME:喂

这表明< IMG方式> 标签确实产生起始和结束的事件

Thanks for reading!

Using XML parsing tutorial from here as a reference, I am trying to parse a simple XML RSS feed with the following structure.

Everything works fine and all values are parsed except for the following case: I am not able to get the content of the <img> tag.


<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>

This is what the endElement() method looks like:


        @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());
        }

Since <img> tag is part of <desc> tag, the code in last else if condition never gets executed.

Note: When I read the the <desc> tag, I could do a manual String search to retrieve the <img> tag content. But, I am sure there has to be a more efficient way.

Can someone guide me on to get content of the <img> tag?

Thanks!

EDIT: Updated the <img> tag. It is now closed correctly.

EDIT2: Updating with startElement() code here. Also updated Feed XML and startElement() code.

@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;
    }       
}

解决方案

The <img> tag actually has no character content, and the value you're after has to be pulled out of the attributes.

To do this, you need to override startElement(String namespaceURI, String localName, String qName, Attributes atts), recognize the <img> tag more or less as you're doing, and get the value you need out of the atts parameter.

Debugging help:

Using this (simple/stupid) handler:

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'
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'

This indicates that the<img> tag does indeed generate start and end events.

这篇关于无法检索嵌套在另一个标记的内容的XML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆