2011-11-07 51 views
2

如何從xml中檢索node_names和attribute_names(不是節點或屬性的值)是否可以使用jquery?從xml中列出xml節點和屬性名稱

<Client> 
    <product name="astro"> 
     <service_process name="ACID"> 
      <host>pable</host> 
      <port>18848</port> 
     </servcice_process> 
     <service_process name="Mestro"> 
      <host>Mico</host> 
      <port>18821</port> 
     </servcice_process> 
    </product> 
</Client> 

回答

3

你可能喜歡的東西this開始:

var xml = '<Client><product name="astro"><service_process name="ACID"><host>pable</host><port>18848</port></servcice_process><service_process name="Mestro"><host>Mico</host><port>18821</port></servcice_process></product></Client>'; 

$(xml).each(function displayChildren (i, node) { 
    console.log(node.nodeName); 

    // traverse attributes 
    $.each(node.attributes, function (j, attr) { 
    console.log(attr.nodeName, attr.nodeValue); 
    }); 

    // recursive call 
    $(node).children().each(displayChildren); 
}); 
1

快速搜索發現this page這與jQuery涉及XML解析。獲取節點名稱有點困難,但提供的方法是here。從一個簡短的看,我假設你想要做的事情沿線︰

$(xml).each(function(){ //xml is the XML resource. 
    if($(this).attr("name")!=""){ 
    alert("Name = " + $(this).attr("name")); 
    } 
    alert("Node = " + $(this).get(0).TagName); 
}); 

我相信這應該沒有問題。