0
   <!--  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. I am not familiar handling XML using javascript. 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>
3
  • 1
    your code is trying to find "site" which is not an element within your xml, nor is there any attributes named "id", if you want the "title" data use .find("title") Commented Sep 21, 2013 at 13:37
  • 1
    You seem to need the content of the title element present within each item element. There are clues there :) Commented Sep 21, 2013 at 13:40
  • Is there any way to do this without using jquery ?? Commented Sep 21, 2013 at 13:50

1 Answer 1

1

Change your success callback code to:

success: function(xml) {
         $(xml).find('item').each(function(){
                 $(this).find('title').each(function(){
         var title = $(this).text();
         alert(title);
       });
       });
    }

And without 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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to do this without using jquery ??
'<html> <head> <title></title> <script type="text/javascript"> function callme() { xmlDoc=loadXMLDoc("sensexrss.xml"); var title = xmlDoc.getElementsByTagName("title")[0]; document.getElementById('aa').innerHTML =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; } </script> </head> <body onload="callme()"> <div id="aa"> </div> </body> </html>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.