Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

DOMParser's parseFromString function and IE 9

DZone's Guide to

DOMParser's parseFromString function and IE 9

· Web Dev Zone
Free Resource

Never build auth again! Okta makes it simple to implement authentication, authorization, MFA and more in minutes. Try the free developer API today! 

When Internet Explorer 9 is running in Standards mode the DOMParser object is now available.

The DOMParser object allows one to convert a string of XML into a document object by calling the DOMParser's parseFromString function.

This article will focus only on the DOMParser object and will not do the standard feature detection tests that should be done for cross-browser compatibility (this code will break if IE 9 is not running in Standards mode or if you are using a previous version of IE).

If you are interested in seeing how to do proper feature detection tests for the DOMParser object, the following article has an example function called ConvertStringToXMLObject:
http://cggallant.blogspot.com/2011/03/xmlhttprequest-calls-when-in-ie-9.html


Something to be aware of with the DOMParser's parseFromString function is that the resulting XML document may have attributes that are rearranged compared to the attributes of the XML string that was passed in.

For example, if we have the following string of XML one would expect that the resulting XML document object would be an exact replica of the original XML string:
var sXML = "<TEST ID=\"124\" TITLE=\"TitleValue\" DATE=\"2011-02-24T00:00:00\" STATUS=\"Processing\" EMPNAME=\"Smith, Sam\" STATUSCODE=\"P\" ROWNUM=\"1\" />"; 

What has been discovered, however, is that if you pass the above XML string into the DOMParser's parseFromString function, the attributes in this case are completely reversed:
var dpParser = new DOMParser();
var xdDoc = dpParser.parseFromString(sXML, "text/xml");

// Grab the root 'Test' node from the document object and then
// loop through the attributes
var xnTest = xdDoc.getElementsByTagName("TEST")[0];
var xaAttrib = null;
var iAttribCount = xnTest.attributes.length;
for (var iIndex = 0; iIndex < iAttribCount; iIndex++){
// Grab the current attribute and display its name
xaAttrib = xnTest.attributes[iIndex];
alert(xaAttrib.nodeName);
}

The result shows that the XML attributes passed in are now in reverse order:
<TEST ROWNUM="1" STATUSCODE="P" EMPNAME="Smith, Sam" STATUS="Processing" DATE="2011-02-24T00:00:00" TITLE="TitleValue" ID="124" />

A bug was filed with Microsoft on this issue but, unfortunately, Microsoft has indicated that the parseFromString behavior is by design.

The best advice that can be given for this situation is to not iterate over the attributes using indexes and instead to grab the attribute values by name as in the following example:
var xnTest = xdDoc.getElementsByTagName("TEST")[0];
var sID = xnTest.getAttribute("ID"); 

Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Published at DZone with permission of Gerard Gallant, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}