无法使用Jquery在XML中找到嵌套标记

时间:2022-06-24 20:21:42

I'm querying an rss feed and getting back some xml. It looks like this:

我正在查询rss feed并找回一些xml。它看起来像这样:

<channel>
  <item>
    <description><![CDATA[<h5>18 Jun 2013: Zambia 2013</h5>
      <p>Welcome Home! <br />Return to Cork Airport.</p>
      <p><img src="http://thedomain.ie/images/pic.JPG"  /></p>
                ]]>
    </description>
  </item>
</channel>

To get the description tag, I do:

要获取描述标记,我会:

$(data).find('item').each(function (index) {

    description = $(this).find('description');
    console.log('description is ');
    console.log(description);

});

This works fine and I can see in the chrome concole the description tag is found. However, when I try to get any nested tags, it never finds them. For example, to find the image tag, I do:

这工作正常,我可以在chrome concole中看到找到描述标签。但是,当我尝试获取任何嵌套标签时,它永远不会找到它们。例如,要查找图像标记,我会:

img = $(this).find('description img');

But the image tag is never found. The same is true for the

但是找不到图像标签。这同样适用于

tag. What am I doig wrong?

标签。我做错了什么?

1 个解决方案

#1


0  

Your description tag contains a CDATA block which is meant to hide data from the parser, so it is not parsed as HTML. You can get the content as text using:

您的description标记包含一个CDATA块,用于隐藏解析器中的数据,因此不会将其解析为HTML。您可以使用以下内容将内容作为文本获取

var descriptionAsText = $(this).find('description').text();

And if you want to traverse the content as HTML, convert to a jQuery object as:

如果您想以HTML格式遍历内容,请转换为jQuery对象:

$(descriptionAsText).find('img')

#1


0  

Your description tag contains a CDATA block which is meant to hide data from the parser, so it is not parsed as HTML. You can get the content as text using:

您的description标记包含一个CDATA块,用于隐藏解析器中的数据,因此不会将其解析为HTML。您可以使用以下内容将内容作为文本获取

var descriptionAsText = $(this).find('description').text();

And if you want to traverse the content as HTML, convert to a jQuery object as:

如果您想以HTML格式遍历内容,请转换为jQuery对象:

$(descriptionAsText).find('img')