开发者

jquery - get xml param

开发者 https://www.devze.com 2023-04-03 21:13 出处:网络
I want to get a xml param (\'fileSrc\') in jquery. What I should do? My jquery code: $xml = $(\"#my_xml_iframe\").contents();

I want to get a xml param ('fileSrc') in jquery.

What I should do?

My jquery code:

$xml = $("#my_xml_iframe").contents();

//convert object to string or search for fileSrc

alert($xml);

My iframe:

<iframe id="my_xml_iframe" src="myFile.xml"></iframe>

My xml (in iframe):

<reponse>

<fileSrc>photo123.jpg</fileSrc>

</response>

I need code for search a object or convert to string. Anyone can help me? btw: sorry for my engl开发者_运维百科ish.


Try this:

// get iframe html code
var xml = $("#my_xml_iframe").contents().find('body').html();
alert(xml);

// use regular expression to get the fileSrc
var pattern = /\<fileSrc\>(.*)\<\/fileSrc\>/i;
var fileSrc = pattern.exec(xml)[1];
alert(fileSrc);

Also see my jsfiddle.


var xml = $("#my_xml_iframe").contents().find('body').html();
console.log(xml);



var v = $(xml).find("fileSrc").text();
alert(v);

but parsing xml with DOM traversal methods is NOT good use instead .parseXML

here is a fiddle http://jsfiddle.net/CTfGe/1/

with .parseXML

var xml = "<reponse><fileSrc>photo123.jpg</fileSrc></reponse>",
    xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $fileSrc= $xml.find( "reponse");

alert($fileSrc.find("fileSrc").text());

DEMO

0

精彩评论

暂无评论...
验证码 换一张
取 消