javascript - How to read xml file contents in jQuery and display in html elements? -
i new jquery.i trying read data "samplexml.xml" file , display data in html "li" elements. far have done is, have created html file follows:file name-"cloudtags.html":
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> </script> <script src=cloudtags.js></script> <title>css globe: tag clouds</title> <link rel="stylesheet" type="text/css" href="cloudtags.css"> <script type="text/javascript" src="js/jquery.js"></script> </head> <body> <div id="container"> <script type="text/javascript" src="http://cssglobe.com/ads/blogsponsor.js"></script> <div id="side"> <div class="tags"> <ul class="cld"> <li class="tag1" id="java"><a href="https://www.google.com">google</a></li> <li class="tag2"><a href="#">chiessy</a></li> <li class="tag3"><a href="#">sitemap</a></li> <li class="tag2"><a href="#">sales</a></li> <li class="tag4" ><a href="#">gohome</a></li> <li class="tag1"id="temp"><a href="#">movies</a></li> <li class="tag1"><a href="#">it jobz</a></li> <li class="tag5"><a href="#">alza</a></li> <li class="tag2"><a href="#">sea food</a></li> <li class="tag1"><a href="#">hospital</a></li> <li class="tag3"><a href="#">smart phone</a></li> <li class="tag4"><a href="#">pizza </a></li> <li class="tag1"><a href="#">aerobics</a></li> <li class="tag5"><a href="#">yahoo...</a></li> <li class="tag1"><a href="#">anti-virus</a></li> <li class="tag2"><a href="#">travel</a></li> </ul> </div> </div> <div id="xmldata"></div> </div><br> </body> </html>
and .js file:
$(document).ready(function() { var nm; $.ajax({ type: "get" , url: "samplexml.xml" , datatype: "xml" , success: function(xml) { $(xml).find('person').each(function() { nm= $(this).text() $("#temp").html(nm); } } }); });
my xml file follows:
<?xml version='1.0' ?> <doc> <person> <name>sachin</name> <age>21</age> </person> <person> <name>akash</name> <age>18</age> </person> </doc>
but not work. need link external file "$.ajax" . ,please tell me i'm making mistake . . in advance . .
i think want this, demo
var xmldoc = $.parsexml( xml ); var $xml = $(xmldoc); var $person = $xml.find("person"); $person.each(function(){ var name = $(this).find('name').text(), age = $(this).find('age').text(); $("#profilelist" ).append('<li>' +name+ ' - ' +age+ '</li>'); });
Comments
Post a Comment