如何将cdata从xml转换为html
我通过 AJAX 接收 XML 数据,并且 XML 包含一些 CDATA 值。
示例:
<xml>
<mytag>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/14.gif"/><br />
<b>Current Conditions:</b><br />
]]>
</mytag>
</xml>
我喜欢在 div 标签处使用 class="title" 来自 b 标签的内容,在 div 标签处使用带有右侧的 img 标签 class="current-img" src
<html>
<body>
<div class="weather">
<div class="title">...</div>
<div class="current-img">...</div>
</div
</body>
</html>
现在我从数据创建了一个 jQuery 对象,并尝试使用 .text() 获取数据,但是如何才能获得特殊数据
var $weather = $(".weather");
$.ajax({
url: "yahooweather.php?w=782458",
context: document.body,
success: function(data){
$xml = $(data).find("xml mytag");
$weather.find(".title").html($xml.children("b")); //this isnt working!
var str = $xml.text()
I'm receiving XML data via AJAX and the XML includes some CDATA values.
Example:
<xml>
<mytag>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/14.gif"/><br />
<b>Current Conditions:</b><br />
]]>
</mytag>
</xml>
and I like to have for the ... at the div-tag with class="title" the content from the b-tag and at the div-tag class="current-img" an img-tag with the right src
<html>
<body>
<div class="weather">
<div class="title">...</div>
<div class="current-img">...</div>
</div
</body>
</html>
for now I created a jQuery Object from the data and tried to get with .text() the data but how can I get a special data just the <img src="..."/>
var $weather = $(".weather");
$.ajax({
url: "yahooweather.php?w=782458",
context: document.body,
success: function(data){
$xml = $(data).find("xml mytag");
$weather.find(".title").html($xml.children("b")); //this isnt working!
var str = $xml.text()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://forum.jquery.com/topic/how-to-convert-xml-with-cdata-to-html-and-back-again#14737000002282604
看起来它取决于正确的内容类型。
http://forum.jquery.com/topic/how-to-convert-xml-with-cdata-to-html-and-back-again#14737000002282604
It looks like it depends on correct content type.