0

Im trying to parse an xml string in IE based on the following example: http://dean.edwards.name/weblog/2006/04/easy-xml/

<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">

    $(function(){

        var xml = document.createElement("xml");
        xml.src = '<test><node1>Test</node1></test>';
        document.body.appendChild(xml);
        var xmlDocument = xml.XMLDocument;
        document.body.removeChild(xml);

    });

</script>
</head>
<body>

</body>
</html>

Fiddle

However, its not working. I get no errors, but nothing is happening.

Is there any way to generate an XML file on the client side in IE based on a valid xml string? Is activeX the only option?

Thanks in advance

Johan
  • 35,120
  • 54
  • 178
  • 293

2 Answers2

4

A variant I have working is not to create an xml object, but create a wrapper div instead:

<script type="text/javascript">
  $(function(){
    var div, xmlDocument;
    div = document.createElement('div');
    div.innerHTML = '<xml><test><node1>Test</node1></test></xml>';
    document.body.appendChild(div);

    xmlDocument = div.firstChild.XMLDocument;
    document.body.removeChild(div);
  });
</script>
Roonaan
  • 1,066
  • 8
  • 11
0

ActiveX is certainly one option. The code would be something like:

var xml = '<test><node1>Test</node1></test>';
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xml);

alert(xmlDoc.documentElement.nodeName);
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • My main problem is that IE doesnt show it as an XML. I suppose its a mime-type issue: Got any ideas how to render the string as an xml file? – Johan May 09 '12 at 17:36
  • @Johan: I don't really understand what you're trying to do, sorry. Could you elaborate? – Tim Down May 09 '12 at 17:41
  • I want the xml string to be opened in a new window, rendered as a regular xml document in IE, not as a HTML page. Just as when you open any XML file with IE. – Johan May 09 '12 at 17:46
  • Ah, OK. I'm not sure it's easily doable. The best I've found are http://stackoverflow.com/questions/5581592/render-xml-document-obtained-through-ajax-call-to-a-new-window and http://www.velocityreviews.com/forums/t678496-default-msxml-xsl-stylesheet-for-ie7.html – Tim Down May 09 '12 at 23:45