<!-- http://www.abc.com -->
<rss version="2.0">
<channel>
<title>SENSEX view</title>
<link>http://www.abc.com</link>
<copyright>Copyright 2009, abc</copyright>
<item>
<title>
<![CDATA[ SENSEX : 20263.71 * -382.93 (-1.85 %) ]]>
</title>
<link/>
<pubDate>9/20/2013 4:00:00 PM</pubDate>
<author>abc</author>
<guid/>
</item>
</channel>
</rss>
我想从变量中的上述XML数据中提取“<![CDATA[ SENSEX : 20263.71 * -382.93 (-1.85 %) ]]>
”。我不熟悉使用javascript处理XML。所以我需要一些帮助或建议我一些教程?
这就是我所拥有的:
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script>
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://rss.xml",
dataType: "xml",.find("title")
success: function(xml) {
$(xml).find("title").each(function(){
var title = $(this).find('title').text();
$(this).find('title').each(function()
{
alert(title );
});
});
}
});
});
</script>
</head>
<body>
</body>
</html>
答案 0 :(得分:1)
将您的success
回调代码更改为:
success: function(xml) {
$(xml).find('item').each(function(){
$(this).find('title').each(function(){
var title = $(this).text();
alert(title);
});
});
}
没有jQuery:
xmlDoc=loadXMLDoc("yourFile.xml");
var title = xmlDoc.getElementsByTagName("title")[0];
alert(title);
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}