returning xml string - how to parse xml file using JQuery/Json -
when alert returning string this:
data "<?xml version="1.0" encoding="utf-8" ?> <xml xmlns="http://www.opengis.net/kml/2.2"> <document> <name>john smith</name> <description>stackoverflow</description> <total>50</total> </document> </xml>"
update: tried using method getjson , alerts never execute inside find('document').each.....
$.getjson(_url, function (data) { alert(data); $(data).find('document').each(function () { debugger var name = $(this).find('name'); var desc = $(this).find('description').text(); var total = $(this).find('total').text() }); });
how read xml file in jquery, below returning me string , can see when alert(data);
$.getjson(url, {}, function (data) { alert(data); } }); <?xml version="1.0" encoding="utf-8" ?> - <xml xmlns="http://www.opengis.net/kml/2.2"> - <document> <name>john smith</name> <description>stackoverflow</description> <total>50</total> </document> </xml>
you appear have misunderstood json is, , how used in jquery!?
if want cross-domain, returned data must in json format. jquery attempt parse json receives it. expects in format "jsonp1291171891383({})" evaluated javascript. xml have returned in not javascript.
one possible way work around return data "jsonp1({"data":"<xml>"})". if case, in example variable "data" plain text, , need parse xml before can access via selector methods.
from here.
jquery.fromxmlstring = function(strxml){ if (window.domparser) { return jquery(new domparser().parsefromstring(strxml, "text/xml")); } else if (window.activexobject) { var doc = new activexobject("microsoft.xmldom"); doc.async = "false"; doc.loadxml(strxml); return jquery(doc); } else { return jquery(strxml); } };
and in code:
$.fromxmlstring(data).find('document').each( ... );
Comments
Post a Comment