使用JS解析xml字符串

时间:2023-02-07 17:02:03

I have an XML string in which Val.responseText gives me

我有一个XML字符串,Val.responseText给了我

<NewDataSet>
  <Table>
    <FieldID>21</FieldID>
    <TableName>F003v001</TableName>
    <FieldName>Grade</FieldName>
    <DisplayField>Grade</DisplayField>
    <FieldType>text</FieldType>
  </Table>
</NewDataSet>

I'm calling FillTable(sVal.responseXML.documentElement);

我正在调用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?

我如何获得fieldid,tablename,displayfield的值?

I tried NodeList = doc.documentElement.selectNodes("Table") but nodelist.length gives me zero.

我尝试了NodeList = doc.documentElement.selectNodes(“Table”),但nodelist.length给了我零。

2 个解决方案

#1


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

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

使用您创建的doc变量而不是documentElement,然后您可以在其上使用这些方法。

#2


You muight find this helpful also:

你也很有帮助:

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

#1


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

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

使用您创建的doc变量而不是documentElement,然后您可以在其上使用这些方法。

#2


You muight find this helpful also:

你也很有帮助:

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