javascript - 在javascript中选择具有特定属性值的节点

标签 javascript xml

在 XML 中:

<elements>
        <product id="1">
                <brand>xxxxxxx</brand>
                <dci>xxxxx</dci>
                <therapeutic_area>xxxxxx</therapeutic_area>
        </product>
        <product id="2">
                <brand>xxxxxx</brand>
                <dci>xxxx</dci>
                <therapeutic_area>xxxx</therapeutic_area>
        </product>
        <product id="3">
                <brand>xxx</brand>
                <dci>xxxx</dci>
                <therapeutic_area>xxxxx</therapeutic_area>
        </product>

我需要选择具有特定属性值的节点。例如2

我尝试了这个,但它不起作用:

alert(xmlDoc.getElementsByTagName("product")[0].getAttributeNode("2"));

预先感谢您的帮助

最佳答案

尝试这样

var list=xmlDoc.getElementsByTagName("product");
for (i=0;i<list.length;i++)
{
     if(list[i].getAttribute("id")==2){
        // Found your node
     }
} 

关于javascript - 在javascript中选择具有特定属性值的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30757317/

相关文章:

javascript - grails gsp javascript集成,如何转义变量?

javascript - 正则表达式在斜杠的第二个实例之后捕获整个字符串

javascript - 字段集的 Angular Group 验证

java - 在 xsd 中找不到引用

php - 如何在 PHP 中将 XML 字符串转换为 DOMDocument?

xml - Groovy Node.depthFirst() 返回节点和字符串列表?

javascript - 如何检测浏览器对 VML 或 SVG 的支持

javascript - node.js & express - 应用程序结构的全局模块和最佳实践

xml - 使用 XElement 时如何包含换行符?

python - 使用python etree提取xml文件的一部分