问答详情
源自:1-1 初次邂逅 XML

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

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

提问者:慕圣0026647 2018-05-24 02:33

个回答

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

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