0

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 but 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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Only one update for jQuery 1.5, as per docs it should be $($.parseXML(output))
@Jaydeep: Oh yes, you're absolutely right. Careless mistake, now fixed.
0

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);
    }

Comments

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.