2011-11-07 68 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); 
}); 

我相信这应该没有问题。