列出xml中的xml节点和属性名

List xml nodes and attribute name from xml

本文关键字:xml 属性 节点 中的 列出      更新时间:2023-09-26

如何从xml中检索node_names和attribute_names(不是节点或属性的值)是否可以使用jquery?

<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>

你可以这样开始:

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);
});

快速搜索显示该页面处理使用jQuery的XML解析。获取节点名称有点困难,但这里提供了获取它的方法。通过快速浏览,我假设您想要执行以下操作:

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

我相信这应该没问题。