我正在使用XMLHttpRequest对象在javascript中创建一个简单的XML解析器,而我只返回我想要的标记时遇到问题。例如,对于this xml文件,我想访问并打印变量数组(第一个),但是我当前的代码不起作用。
的JavaScript
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
function run() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://www.deadstock.ca/collections/adidas/products/adidas-equipment- support-adv-core-black-24.xml", true);
xhttp.send();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
// Typical action to be performed when the document is ready:
console.log(xhttp.responseText.hash.variant[0]);
}
}
}
run();
然而,这可行(返回所有内容)
console.log(xhttp.responseText);
我目前正在使用node运行该文件。