gpt4 book ai didi

javascript - 从 xml 中列出 xml 节点和属性名称

转载 作者:行者123 更新时间:2023-11-27 22:45:00 25 4
gpt4 key购买 nike

如何从 xml 中检索节点名称和属性名称(不是节点或属性的值)是否可以使用 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>

最佳答案

您可以从类似 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);
});

关于javascript - 从 xml 中列出 xml 节点和属性名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8034503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com