从xml文件获取数据并使用Ajax和xpath显示在html表中

时间:2022-02-08 03:44:56

Here is how i started with

这是我开始的方式

<script>
$(document).ready(function() {

    $.ajax({
        type : "GET",
        url : "EmployeeData.xml",
        dataType : "xml",
        success : processXml
    });
});

// function to process the read in XML
function processXml(xml) {
    var nodes = xmlDoc.selectNodes("/employeelist/employee");

          // Help Here
    }</script>

xml file

<employeelist>
<employee>
    <id>01</id>
    <name>Bob</name>
    <gender>M</gender>
    <designation>Traniee</designation>
    <salary>18000</salary>
    <doj>01-03-2012</doj>
</employee>
<employee>
    <id>02</id>
    <name>Rob</name>
    <gender>M</gender>
    <designation>Manager</designation>
    <salary>40000</salary>
    <doj>04-03-2010</doj>
</employee></employeelist>

I want to use xpath to traverse the xml file and get all the elements and child node values. Place where i stuck is , how to loop the node to get child node values.

我想使用xpath遍历xml文件并获取所有元素和子节点值。我卡住的地方是,如何循环节点以获取子节点值。

thanks..!!

2 个解决方案

#1


0  

 xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $employee= $xml.find( "employee" );

reference parse xml

引用解析xml

#2


0  

Here is the code i used

这是我使用的代码

for ( var i = 0; i < nodes.length; i++) {
var id = nodes[i].selectSingleNode("id").firstChild.nodeValue;
var name = nodes[i].selectSingleNode("name").firstChild.nodeValue;
var designation = nodes[i].selectSingleNode("designation").firstChild.nodeValue;
}

#1


0  

 xmlDoc = $.parseXML( xml ),
    $xml = $( xmlDoc ),
    $employee= $xml.find( "employee" );

reference parse xml

引用解析xml

#2


0  

Here is the code i used

这是我使用的代码

for ( var i = 0; i < nodes.length; i++) {
var id = nodes[i].selectSingleNode("id").firstChild.nodeValue;
var name = nodes[i].selectSingleNode("name").firstChild.nodeValue;
var designation = nodes[i].selectSingleNode("designation").firstChild.nodeValue;
}