[英]Extracting XML data using javascript
<!-- 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>
I want to extract " <![CDATA[ SENSEX : 20263.71 * -382.93 (-1.85 %) ]]>
" from above XML data in a variable. 我想从变量中的XML数据中提取“ <![CDATA[ SENSEX : 20263.71 * -382.93 (-1.85 %) ]]>
“。 I am not familiar handling XML using javascript. 我不熟悉使用javascript处理XML。 So i need some help or suggest me some tutorials ?? 所以我需要帮助或建议我一些教程??
Here's what i have got : 这是我得到的:
<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>
Change your success
callback code to: 将您的success
回调代码更改为:
success: function(xml) {
$(xml).find('item').each(function(){
$(this).find('title').each(function(){
var title = $(this).text();
alert(title);
});
});
}
And without jQuery: 如果没有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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.