gpt4 book ai didi

javascript - JS中XML节点的解析

转载 作者:行者123 更新时间:2023-11-29 21:32:41 24 4
gpt4 key购买 nike

我正在尝试用 JS 解析 XML 文件,下面的 XML 包含父元素和子元素

我的 XML 文件:

<?xml version="1.0" ?>
<root>
<program name="John">
<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>
<module type="a3">
<modPath>bbbbbbbbbb</modPath>
<modInputTemplate>bbbbbbbbbb</modInputTemplate>
<modSchematic>bbbbbbbb</modSchematic>
</module>
<module type="a4">
<modPath>cccc</modPath>
<modInputTemplate>cccccc</modInputTemplate>
<modSchematic>ccccccc</modSchematic>
</module>
<module type="a5">
<modPath>ddddddd</modPath>
<modInputTemplate>ddddddddddddd</modInputTemplate>
<modSchematic>dddddddddd</modSchematic>
</module>
<module type="a6">
<modPath>eeee</modPath>
<modInputTemplate>eee</modInputTemplate>
<modSchematic>eee</modSchematic>
</module>
<module type="a7">
<modPath>ffff</modPath>
<modInputTemplate>ffff</modInputTemplate>
<modSchematic>fffff</modSchematic>
</module>
<module type="a8">
<modPath>ggggggg</modPath>
<modInputTemplate>ggggggg</modInputTemplate>
<modSchematic>ggggggg</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>
<module type="b3">
<modPath>nnnn</modPath>
<modInputTemplate>nnnnnn</modInputTemplate>
<modSchematic>nnnn</modSchematic>
</module>
<module type="b4">
<modPath>oooooo</modPath>
<modInputTemplate>ooooooo</modInputTemplate>
<modSchematic>oooooooooooo</modSchematic>
</module>
<module type="b5">
<modPath>pppppppppp</modPath>
<modInputTemplate>ppppppppppp</modInputTemplate>
<modSchematic>ppppppppppp</modSchematic>
</module>
<module type="b6">
<modPath>qqqqqqqqq</modPath>
<modInputTemplate>qqqqqqqqqq</modInputTemplate>
<modSchematic>qqqqqqqqqqq</modSchematic>
</module>
</computation>
</program>
<program name="David">
.......
.......
.......
</program>
<program name="Smith">
.......
.......
.......
</program>
</root>

JavaScript 文件

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

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>
</body>
</html>

输出我得到的只是a1内容:

modPath:aaa

modInputTemplate:aaaaaaaa

modSchematic:aaaa

I want for all the childs (a1 to a8)

要求:

1)如果我传递一个父属性值如“na​​me=John”,那么我应该得到它的所有子节点如下

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

2)同样,如果我将属性作为 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>
.
.
.
<module type="a8">
<modPath>ggggggg</modPath>
<modInputTemplate>ggggggg</modInputTemplate>
<modSchematic>ggggggg</modSchematic>
</module>

通过传递属性值在节点上方打印的逻辑是什么?

最佳答案

使用 jQuery:

function loadxml() {
$.get('writers.xml', readXML);
}
function readXML(xml) {
var txt = '';
$(xml).find('computation[type=student] module').each(function() {
txt += $(this).find('modPath').text() + '<br/>';
});
$('#demo').html(txt);
}

关于javascript - JS中XML节点的解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767735/

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