如何根据属性获取XML父标记的子节点

时间:2022-11-27 07:35:25

I have been trying to display all child nodes of the parent node based on the attribute type using JS

我一直在尝试使用JS基于属性类型显示父节点的所有子节点

Requirement:

user gives three I/P as :

用户给出三个I / P:

1)"Ram" "Student" "a1"

1)“Ram”“学生”“a1”

O/P :should display all the child elements based on name & type selected

O / P:应根据所选的名称和类型显示所有子元素

aaa,aaaaaaaa,aaaa

2)"Ram" "Student" "a2" :

2)“Ram”“学生”“a2”:

xxxx,tttttt,yyyy

XML:

<?xml version="1.0" ?>
<root>
    <program name="Ram">
        <computation type="student">
            <module type="a1">              
                <modPath>aaa</modPath>
                <modInputTemplate>aaaaaaaa</modInputTemplate>
                <modSchematic>aaaa</modSchematic>
            </module>
            <module type="a2">              
                <modPath>xxxx</modPath>
                <modInputTemplate>tttttt</modInputTemplate>
                <modSchematic>yyyy</modSchematic>
            </module>
        </computation>
        <computation type="Employee">
            <module type="b1">              
                <modPath>lllll</modPath>
                <modInputTemplate>llllll</modInputTemplate>
                <modSchematic>lllll</modSchematic>
            </module>
            <module type="b2">              
                <modPath>mmmmmmmmm</modPath>
                <modInputTemplate>mmmmmmmm</modInputTemplate>
                <modSchematic>mmmmmm</modSchematic>
            </module>
        </computation>      
    </program>
    <program name="Rahul">
    .......
    .......
    .......
    </program>
    <program name="Ramesh">
    .......
    .......
    .......
    </program>
</root>

I have JS to display the Child nodes ,But its not based on the attribute value

我有JS来显示Child节点,但它不是基于属性值

    <html>
<head>
<title>Read XML in Microsoft Browsers</title>
<script type="text/javascript">
    var xmlDoc;
    function loadxml()
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = false;
        xmlDoc.onreadystatechange = readXML;
        xmlDoc.load("writers.xml");
    }


    function readXML()
    {
        if(xmlDoc.readyState == 4){
            myFunction(xmlDoc);
        }
        function myFunction(xml) {
            var x, i, txt;
            txt = "";

            var x = xmlDoc.getElementsByTagName("module");//Here "module" should be replaced by attribute value which user gives as i/p

            for( i = 0; i < x[0].childNodes.length; i++) {
                txt += x[0].childNodes[i].nodeName + ": " + x[0].childNodes[i].childNodes[0].nodeValue + "<br>";
            }
            document.getElementById("demo").innerHTML = txt;
        }
    }
</script>
</head>

<body onload="loadxml()">
    <p id="demo">Write output of loadxml()</p>
    <p id="test">Test me!</p>
</body>

</html>

2 个解决方案

#1


0  

All you have to do is to check for the desired attribute.

您所要做的就是检查所需的属性。

Your code gives back an array of modules:

您的代码返回了一系列模块:

var x = xmlDoc.getElementsByTagName("module");

The last thing for you to do is to go through the nodes and select only those with the desired attribute with:

您要做的最后一件事是遍历节点并仅选择具有所需属性的节点:

// x is the array of modules, i stands for the array iteration    
x[i].getAttribute(name);

Now you can combine your desired input with the search algorithm. Since you got Childnodes with Children it could get a little slow. You will always go through:

现在,您可以将所需的输入与搜索算法相结合。既然你有孩子的Childnodes,它可能会有点慢。你将永远经历:

Parent -> Child.getAttribute -> new array -> Child.getAttribute.

The last thing for you to do is to check for the input with a normal if statement, it depends on how complicated your input is.

您要做的最后一件事是使用正常的if语句检查输入,这取决于您的输入有多复杂。

If anything is unclear, feel free to ask.

如果有什么不清楚,请随时问。

Regards, Megajin

#2


0  

You can use XPath to locate part of XML document using complex filter. Example below shows XPath expression that will return all children elements of module, where type and name attributes of itself as well as the ancestors, match certain value :

您可以使用XPath使用复杂的过滤器来定位XML文档的一部分。下面的示例显示了将返回模块的所有子元素的XPath表达式,其中自身的类型和名称属性以及祖先匹配特定值:

var result = "";

var name = "Ram";
var type1 = "student";
var type2 = "a2";
var query = "/root/program[@name='" + name + "']/computation[@type='" + type1 + "']/module[@type='" + type2 + "']/*";

var nodes = xmlDoc.selectNodes(query);
for (i = 0; i < nodes.length; i++) {
    var node = nodes[i];
    result += node.nodeName + ": " + node.childNodes[0].nodeValue + "<br>";
}

document.getElementById("demo").innerHTML = result;

Tested on IE11 and it shows the expected output after clicking on "Allow blocked content" prompt :

在IE11上测试,它在点击“允许阻止的内容”提示后显示预期的输出:

如何根据属性获取XML父标记的子节点

Output :

如何根据属性获取XML父标记的子节点

#1


0  

All you have to do is to check for the desired attribute.

您所要做的就是检查所需的属性。

Your code gives back an array of modules:

您的代码返回了一系列模块:

var x = xmlDoc.getElementsByTagName("module");

The last thing for you to do is to go through the nodes and select only those with the desired attribute with:

您要做的最后一件事是遍历节点并仅选择具有所需属性的节点:

// x is the array of modules, i stands for the array iteration    
x[i].getAttribute(name);

Now you can combine your desired input with the search algorithm. Since you got Childnodes with Children it could get a little slow. You will always go through:

现在,您可以将所需的输入与搜索算法相结合。既然你有孩子的Childnodes,它可能会有点慢。你将永远经历:

Parent -> Child.getAttribute -> new array -> Child.getAttribute.

The last thing for you to do is to check for the input with a normal if statement, it depends on how complicated your input is.

您要做的最后一件事是使用正常的if语句检查输入,这取决于您的输入有多复杂。

If anything is unclear, feel free to ask.

如果有什么不清楚,请随时问。

Regards, Megajin

#2


0  

You can use XPath to locate part of XML document using complex filter. Example below shows XPath expression that will return all children elements of module, where type and name attributes of itself as well as the ancestors, match certain value :

您可以使用XPath使用复杂的过滤器来定位XML文档的一部分。下面的示例显示了将返回模块的所有子元素的XPath表达式,其中自身的类型和名称属性以及祖先匹配特定值:

var result = "";

var name = "Ram";
var type1 = "student";
var type2 = "a2";
var query = "/root/program[@name='" + name + "']/computation[@type='" + type1 + "']/module[@type='" + type2 + "']/*";

var nodes = xmlDoc.selectNodes(query);
for (i = 0; i < nodes.length; i++) {
    var node = nodes[i];
    result += node.nodeName + ": " + node.childNodes[0].nodeValue + "<br>";
}

document.getElementById("demo").innerHTML = result;

Tested on IE11 and it shows the expected output after clicking on "Allow blocked content" prompt :

在IE11上测试,它在点击“允许阻止的内容”提示后显示预期的输出:

如何根据属性获取XML父标记的子节点

Output :

如何根据属性获取XML父标记的子节点