Parse xml string with JS

I have an xml string

sVal.responseText gives me

< NewDataSet >

  < Table >

    <FieldID>21</FieldID>

    <TableName>F003v001</TableName>

    <FieldName>Grade</FieldName>

    <DisplayField>Grade</DisplayField>

    <FieldType>text</FieldType>

  < /Table >

</NewDataSet>

      

I am calling FillTable (sVal.responseXML.documentElement);

function FillTable(sResponse) {

    var preXML = sResponse;

    // code for IE
    if (window.ActiveXObject) {
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(preXML);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else {
        var parser = new DOMParser();
        var doc = parser.parseFromString(preXML, "text/xml");
    }

    // documentElement always represents the root node
    var x = doc.documentElement;

}

      

Now I want to parse each of the node and populate the datagrid. Can anyone help me parse the nodes?

How to get values ​​for fieldid, tablename, displayfield?

I've tried NodeList = doc.documentElement.selectNodes ("Table")

but nodelist.length gives me zero.

Please help Thanks

0


source to share


2 answers


http://www.w3schools.com/Dom/dom_methods.asp



use the created doc variable instead of documentElement then you can use these methods on it.

+1


source


You will also find this useful:



http://www.hiteshagrawal.com/javascript/javascript-parsing-xml-in-javascript

+1


source







All Articles