简体   繁体   English

在XML文档中搜索某个元素/属性值,然后返回该元素

[英]Searching an XML doc for a certain element/attribute value and return that element

So I got this code which gets me the elements from an XML(a product catalog) and lists them in a table. 因此,我得到了这段代码,该代码使我从XML(产品目录)中获取了元素并将其列出在表中。 What I want to do now is to return from that XML only 1 product with all it's elements. 我现在想做的是从该XML中仅返回具有所有元素的一种产品。 For example, I want to get from the catalog only the product I want to see that has a certain SERIAL. 例如,我只想从目录中获得要查看的具有特定SERIAL的产品。

This is my XML document: 这是我的XML文档:

<CATALOG>
    <PRODUCT>
       <SERIAL>123ABC</SERIAL>
       <PRODNR>1234</PRODNR>
       <PRODNM>COOLER</PRODNM>
       <ACCNAME>JOHN</ACCNAME>
       <NRDOC>0001</NRDOC>
    </PRODUCT>
    <PRODUCT>
       <SERIAL>234BCD</SERIAL>
       <PRODNR>2345</PRODNR>
       <PRODNM>MOUSEPAD</PRODNM>
       <ACCNAME>STEVE</ACCNAME>
       <NRDOC>0002</NRDOC>
    </PRODUCT>
    <PRODUCT>
       <SERIAL>345CDE</SERIAL>
       <PRODNR>3456</PRODNR>
       <PRODNM>KEYBOARD</PRODNM>
       <ACCNAME>WILLIAM</ACCNAME>
       <NRDOC>0003</NRDOC>
    </PRODUCT>
    <PRODUCT>
       <SERIAL>456DEF</SERIAL>
       <PRODNR>4567</PRODNR>
       <PRODNM>MOUSE</PRODNM>
       <ACCNAME>MARCUS</ACCNAME>
       <NRDOC>0004</NRDOC>
    </PRODUCT>
</CATALOG>

And this is the code I have so far: 这是我到目前为止的代码:

function loadXMLDoc(url) {
var serialNumber = document.getElementById('searchBox').value;
var xmlhttp;
var txt, xx, x, i;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        txt = "<table border='1'><tr><th>Product number</th><th>Product name</th><th>Account name</th><th>Document number</th></tr>";
        x = xmlhttp.responseXML.documentElement.getElementsByTagName("PRODUCT");
        for (i = 0; i < x.length; i++) {
            txt = txt + "<tr>";
            xx = x[i].getElementsByTagName("PRODNM");
            {
                try {
                    txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                }
                catch (er) {
                    txt = txt + "<td>&nbsp;</td>";
                }
            }
            xx = x[i].getElementsByTagName("PRODNR");
            {
                try {
                    txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                }
                catch (er) {
                    txt = txt + "<td>&nbsp;</td>";
                }
            }
            xx = x[i].getElementsByTagName("ACCNAME");
            {
                try {
                    txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                }
                catch (er) {
                    txt = txt + "<td>&nbsp;</td>";
                }
            }
            xx = x[i].getElementsByTagName("NRDOC");
            {
                try {
                    txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                }
                catch (er) {
                    txt = txt + "<td>&nbsp;</td>";
                }
            }
            txt = txt + "</tr>";
        }
        txt = txt + "</table>";
        document.getElementById('showTable').innerHTML = txt;
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

} }

I would use XPath to select necessary PRODUCT node: 我将使用XPath选择必要的PRODUCT节点:

var path = '//SERIAL[text()="' + serialNumber + '"]/..';
var x = document.evaluate(path, xmlhttp.responseXML.documentElement, null, 9, null).singleNodeValue;

Note: ( document.selectNodes(xpath) for IE) 注意:( IE的document.selectNodes(xpath)

Demo: http://plnkr.co/edit/9hp6kM5OT8LEYrTCrCqN?p=preview 演示: http : //plnkr.co/edit/9hp6kM5OT8LEYrTCrCqN?p=preview

Or you can simple loop through all PRODUCT nodes and filter the one with search ID. 或者,您可以简单地遍历所有PRODUCT节点,并使用搜索ID过滤一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM