Question

I am trying to read a XML file to an object using JavaScript to ultimately parse it for information and transform the data in it.

The XML file is well formed (and in the same domain), I pasted it to several validators and all came back without errors, so this shouldn't be the source of the error.

I am trying to access the file via ajax / xmlHttpRequest. Here's a code snippet of the relevant part:

if (xmlHttp)
    {
        var url = xmlFile;
        xmlHttp.open("GET", url, true);
        xmlHttp.onreadystatechange = function() 
        {
            if (xmlHttp.readyState == 4)
            {
                //alert(xmlHttp.responseText);
                var xmlData = xmlHttp.responseXML;
                var txt="";
                var x=xmlData.getElementsByTagName("rs:data");

                alert(x.length);

                for (i=0;i<x.length;i++)
                {
                    txt=txt + x[i].childNodes[0].nodeValue + "<br>";
                }   
            }
        }
        xmlHttp.send();
    }

Now the problem is that the variable "x" always comes back empty (length = 0). When I alert the responseText however, I can see the whole xml file in the alert message, which seems rather strange to me.

The xml file basically looks like this:

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
 xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
 xmlns:rs='urn:schemas-microsoft-com:rowset'
 xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:CommandTimeout='30'>
  <s:AttributeType name='ows_Art' rs:name='Art' rs:number='1'>
     <s:datatype dt:type='string' dt:maxLength='512' />
  </s:AttributeType>
</s:ElementType>
</s:Schema>
  <rs:data>
    <z:row [...] />
  </rs:data>
</xml>

What I want is all the data stored under the node and from what I could learn

var x=xmlData.getElementsByTagName("rs:data");

should do exactly that. The only reason I can think of is that .responseXML comes back empty, however, there is no definite way to check, because if I alert(xmlHttp.reponseXML) it just tells me, that an object is returned, but not the contents of it...

Does anyone have an idea why this whole thing doesn't work? What I find most confusing is, that the .responseText works fine...

Was it helpful?

Solution

rs:data is not the tag name. The tag name is data in the urn:schemas-microsoft-com:rowset namespace (as specified by xmlns:rs='urn:schemas-microsoft-com:rowset').

You should use a namespace-aware DOM method.

var x = xmlData.getElementsByTagNameNS("urn:schemas-microsoft-com:rowset", "data");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top