开发者

Read xml in browser

I have below string in javascript

var output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><abc><xyz><xyzResponse><URL>http%3A%2F%2Flocalhost%3A8080%2Fnet%2Fxyz.do%3Fpartner%3Ddummy%26id%3Dba0e245f-ae67-40b6-986d-3242acea4c04</URL><StatusMsg>SUCCESS</StatusMsg><ID>hello.com</ID><AID>test</AID></xyzResponse></xyz></abc>';

I want to parse this as xml and get values out of it.

I have tried below code

var xmlObj = $(output);
alert(xmlObj.find('URL').text())

It works in FireFox but does not work in IE. It does not give any error b开发者_如何转开发ut does not show any content.

How to read xml that is string format and use content using javascript across the browsers?

Any help is appreciated.


jQuery's $() function doesn't parse XML: it treats it as HTML and inserts it into the HTML DOM, which doesn't work in general. If you're using jQuery 1.5, you can use its new parseXML() method:

var xmlObj = $.parseXML(output);
alert( $(xmlObj).find('URL').text() );

If you can't use jQuery 1.5, you'll need an XML parsing function such as the one I posted here: Strange jQuery XML problem


I did the following for parsing xml for all browsers. I hope you will find it helpful too.

if(window.DOMParser)//Firefox, Chrome and others Browsers
    {
        var xmlString = (new XMLSerializer()).serializeToString(response);
        parser=new DOMParser();
        xmlDoc=parser.parseFromString(xmlString,"text/xml");
    }
    else // Internet Explorer
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async="false";
        xmlDoc.load(response);
    }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜