用JS解析XML文件a,获取item里所有内容,每一个item的内容作为一个数组,例如var a='<item><WERKS>8021</WERKS></item><item><WERKS>8022</WERKS></item>'
时间: 2023-12-15 07:07:27 浏览: 99
JS解析json数据并将json字符串转化为数组的实现方法
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(a, "text/xml");
var items = xmlDoc.getElementsByTagName("item");
var itemContents = [];
for (var i = 0; i < items.length; i++) {
var item = items[i];
var content = {};
var children = item.childNodes;
for (var j = 0; j < children.length; j++) {
var child = children[j];
if (child.nodeType == Node.ELEMENT_NODE) {
content[child.nodeName] = child.textContent;
}
}
itemContents.push(content);
}
console.log(itemContents); //输出 [{WERKS: "8021"}, {WERKS: "8022"}]
阅读全文