使用 jquery 或 javascript 解析 cdata 中的 html
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14961044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
parse html inside cdata using jquery or javascript
提问by Bahamut
I'm getting a website feed that looks like this
我收到了一个看起来像这样的网站提要
<rss...>
<title> some title </title>
<content>
<![CDATA[ <div>this tag is ignored<div> who took the cookie in the cookie jar!? ]]>
</content>
</rss>
I need the entire content of the cdata to be displayed in the html. I'm using jquery 1.9.1 and when I get the content part using $(xml).find('rss content').text()
,
it actually ignores the whole <div>this tag is ignored<div>
part. Any way to get everything inside the CDATA using javascript or jquery?
我需要在 html 中显示 cdata 的全部内容。我正在使用 jquery 1.9.1,当我使用 获取内容部分时 $(xml).find('rss content').text()
,它实际上忽略了整个<div>this tag is ignored<div>
部分。有什么方法可以使用 javascript 或 jquery 获取 CDATA 中的所有内容?
回答by Frédéric Hamidi
Chances are your markup is not parsed as XML by jQuery. Try explicitly invoking $.parseXML():
jQuery 可能不会将您的标记解析为 XML。尝试显式调用$.parseXML():
var contentText = $($.parseXML(xml)).find("rss content").text();
回答by tony gil
bottomline:
底线:
xmlDoc.getElementsByTagName("content")[0].childNodes[0].nodeValue
this snippet from working code uses jquery to load xml and then gets the 4th occurence of the content tag (which contains CDATA)
工作代码中的这个片段使用 jquery 加载 xml,然后获取第 4 次出现的内容标签(其中包含 CDATA)
var req = new AjaxRequest();
req.setMethod("POST");
...
req.loadXMLDoc(linkString, paramString);
var htmlContent = req.getResponse().responseXML.getElementsByTagName('content').item(3).childNodes[0].nodeValue;
回答by Explosion Pills
jQuery is not the bestat parsing XML documents from a string. It would be better to use the browser's native DOM Parser. jQuery can then work with the parsed XML document much better; otherwise I believe it will try to work with it like XML which produces weird results.
jQuery在从字符串解析 XML 文档方面并不是最好的。最好使用浏览器的原生 DOM Parser。jQuery 可以更好地处理解析后的 XML 文档;否则我相信它会尝试像 XML 一样使用它,这会产生奇怪的结果。
$xml = $((new DOMParser).parseFromString(xml, "text/xml"));
http://jsfiddle.net/ExplosionPIlls/2MJt9/
http://jsfiddle.net/ExplosionPIlls/2MJt9/
EDIT: based on the other answer, $.parseXML
is possibly a better option since it should work with other browsers, but you would have to use it in a similar fashion to the above since the result is an XML document rather than a jQuery object.
编辑:基于另一个答案,$.parseXML
可能是更好的选择,因为它应该适用于其他浏览器,但您必须以与上述类似的方式使用它,因为结果是 XML 文档而不是 jQuery 对象。
$xml = $($.parseXML(xml));
回答by Ivan Sitnikov
text from CDATA in jQuery can be retrieved by get first of childNodes data:
jQuery 中来自 CDATA 的文本可以通过获取第一个 childNodes 数据来检索:
!$.ajax(YOUR_URL,{
dataType: 'xml',
success: function (dataR, textStatus, jqXHR){
var rrsobj = $(dataR).find('rss');
if(rrsobj ){
desc = $(rrsobj [0]).children('content');
if(desc)
var txt = desc[0].childNodes[0].data; }
}});
where dataR was read from YOUR_URL and txt contains info from CDATA
其中 dataR 是从 YOUR_URL 读取的,txt 包含来自 CDATA 的信息