Tuesday, March 15, 2011

Parse through xml string using 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 through each of the node and populate a datagrid. Can anyone help me parse through the nodes?

How do i get the values for fieldid,tablename,displayfield?

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

but nodelist.length gives me zero.

Please help Thanks

From stackoverflow

0 comments:

Post a Comment