怎样把两个xml文件都在一个html网页上显示出来呢?我写的只会执行最后一个。

来源:1-1 初次邂逅 XML

慕圣0026647

2018-05-24 02:33

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">

</head>
<body>
<h1>Product List: </h1>
<p id="categories"></p >
<p id="products"></p >

<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myFunction(this);
} };
xhttp.open("GET", "categories.xml", true);
xhttp.send();

function myFunction(xml) {
var xmlDoc = xml.responseXML;
var txt = "";
x = xmlDoc.getElementsByTagName("Categories")[1];
var categories = get_categories(x);
document.getElementById("categories").innerHTML = categories.nodeName;
document.getElementById("categories").innerHTML = "categories: " + categories.nodeName + " " + categories.firstChild.nodeValue;
return categories;
}

function get_categories(n){
var y = n.firstChild;
while (y.nodeType !=1){
y = y.nextSibling;
}
return y;
}


</script>

<script>
var xhttpx = new XMLHttpRequest();
xhttpx.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200){
myFunction(this);
}
}
xhttpx.open("GET", "products.xml",true);
xhttpx.send();

function myFunction(xml) {
var xmlDoc = xml.responseXML;
var txt = "";
x = xmlDoc.getElementsByTagName("Products")[1];
var products = get_products(x);
document.getElementById("products").innerHTML = products.nodeName;
document.getElementById("products").innerHTML = "products: "+ products.nodeName + " " +products.firstChild.nodeValue;
return products;
}
function get_products(n){
var z = n.lastChild;
while (z.nodeType !=1) {
z = z.previousSibling;
}
return z;
}
</script>



</body>
</html>

写回答 关注

1回答

  • 情不知所起丶
    2018-06-03 10:10:03

    两个myFunction冲突了,把第一个改成别的名字

Java眼中的XML---文件读取

通过Java认识并且创造XML文件,如何应用 Java“解析 XML

83199 学习 · 431 问题

查看课程

相似问题