<!-- 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>
Voglio estrarre " <![CDATA[ SENSEX : 20263.71 * -382.93 (-1.85 %) ]]>
" dall'alto dei dati XML in una variabile. Non ho familiarità con la gestione di XML utilizzando javascript. Quindi ho bisogno di qualche aiuto o mi suggerisce alcuni tutorial?
Ecco quello che ho:
<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>
Modifica il codice di richiamata di success
su:
success: function(xml) { $(xml).find('item').each(function(){ $(this).find('title').each(function(){ var title = $(this).text(); alert(title); }); }); }
E senza 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; }