0

i am using below code to traverse xml in javascript i am able to get value using x[i].childNodes[0].text; function in IE but not in firefox. please tell me what should i use so that it work with all.

  function loadXMLDoc(dname) {
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET", dname, false);
    xhttp.send();
    return xhttp.responseXML;
}
function loadxml() {
    var xmlDoc = loadXMLDoc("xmldata/wfc20100915.xml");


    // documentElement always represents the root node
    x = xmlDoc.documentElement.childNodes;
    var arr = new Array();
    var str = "";
    for (i = 0; i < x.length; i++) {
        if (x[i].childNodes.length > 3) {
            arr[i] = new Array(5);
            arr[i][0] = x[i].childNodes[0].text;
            arr[i][1] = x[i].childNodes[1].text;
            arr[i][2] = x[i].childNodes[2].text;
            arr[i][3] = x[i].childNodes[3].text;
            arr[i][4] = x[i].childNodes[4].text;
0

1 Answer 1

1

Modern browsers (Opera, Chrome, Firefox, Safari) and Internet Explorer interpret child nodes in different ways. In code:

<div id="first">
    <div>bla</div>
</div>

( http://jsfiddle.net/2Ra3B/ )

Internet Explorer will see only 1 child node of "first". Modern browsers will see 3 child nodes: 1 element node (div) and 2 text nodes before and after element node (white spaces). Check nodeType == 3 if you want only text nodes.

Moreover, maybe you should use data or nodeValue instead of text (I don't know if text even works, in "classic" browser DOM it doesn't work, but I didn't check it with responseXML).

You should also consider using JSON if you want use transfered data only in JavaScript.

PS. You made mistake in code: checking if childNodes.length > 3 instead of > 4 or better looking >= 5.

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

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.