我正在尝试解析XML文档并使用数据来构建这种形式的(简单)json对象:
{id: '1', name: 'content-types', children: [{id: '2', name: 'requirements': children: [... and so on ...]]}
我的XML具有如下所示的节点(我仅包括其中一个-它们可以任意嵌套):
<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head/>
<body class="taxonomies">
<div class="taxonomy">
<span class="id">3484069771</span>
<span class="name">Content Types</span>
<span class="locale">en</span>
<div class="concepts">
<div class="concept">
<span class="id">3484058507</span>
<span class="name">Promotional Publications</span>
<div class="concepts">
<div class="concept">
<span class="id">3551765771</span>
<span class="name">Datasheets</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
我使用以下代码从XML构建JSON树:
buildConceptTree: function(xml){
const doc = new dom().parseFromString(xml)
var tree = []
var selector = "//*[@class='taxonomies']"
var count = 0 // this should keep track of the depth of the node being used
function recurse(s, odd){
var nodes
console.log(count)
console.log(s)
var arr = []
nodes = xpath.select(s, doc)
nodes.forEach(node => {
try {
var children = node.childNodes
var keys = Object.keys(children).filter(x => {return Number(x)})
keys.forEach(key => {
var child = children[key]
console.log('child is: ')
console.log(child)
var obj = {}
var grandchildren = child.childNodes
var grandkeys = Object.keys(grandchildren).filter(x => {return Number(x)})
grandkeys.forEach(gk => {
var gc = grandchildren[gk]
try {
var nodevalue = gc['attributes'][0]['nodeValue']
switch(nodevalue){
case 'id':
obj['id'] = gc['textContent']
case 'name':
obj['name'] = gc['textContent']
case 'concepts':
count++
var rx = /taxonomy/
if(!rx.test(s)){
s = s+"/*[@class='taxonomy']"
}
else{
s = s
}
if (!odd){
s += "/*[@class='concepts']"
}
else {
s += "/*[@class='concept']"
}
odd = !odd
obj['children'] = recurse(s, odd)
}
}
catch(e){
}
})
arr.push(obj)
})
}
catch(e){
}
})
return arr
}
var tree = recurse(selector, false)
return tree
},
就目前而言,此函数产生的内容类似于我提到的JSON形式,但是缺少许多节点。
此外,似乎我的递归函数并没有在最简单的情况下终止,因为它沿着xml树的更深的分支递归。我在控制台中记录了以下内容(例如),但是没有深度为191度的节点:
191
parser.js?d3c4:83 //*[@class='taxonomies']/*[@class='taxonomy']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']/*[@class='concept']/*[@class='concepts']
parser.js?d3c4:92 child is:
有人可以帮我弄清楚如何更改此功能以使其获取我想要的数据吗?
答案 0 :(得分:1)
我可能已经错过了一些要求,但是一旦停止循环浏览所有元素并开始查询所需的确切元素,问题就会变得不那么复杂了
// Parse the xml string to a document
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(
getXML(),
"text/xml"
);
// The main logic to go from an xml element to an object
const parseTaxonomy = (taxonomy, id = 1) => ({
id,
name: taxonomy.querySelector(".name")
.innerText
.toLowerCase()
.replace(/\s/g, "-"),
children: Array.from(
(taxonomy.querySelector(".concepts") || { children: [] })
.children
).map(t => parseTaxonomy(t, ++id)) // Note the ++
});
// Run on the first taxonomy
// If the top level contains multiple elements, use .map
console.log(
parseTaxonomy(
xmlDoc.querySelector(".taxonomy")
)
);
// The data
function getXML() {
return `<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head/>
<body class="taxonomies">
<div class="taxonomy">
<span class="id">3484069771</span>
<span class="name">Content Types</span>
<span class="locale">en</span>
<div class="concepts">
<div class="concept">
<span class="id">3484058507</span>
<span class="name">Promotional Publications</span>
<div class="concepts">
<div class="concept">
<span class="id">3551765771</span>
<span class="name">Datasheets</span>
</div>
</div>
</div>
</div>
</div>
</body>
</html>`;
};
注意:我更改了您放置评论的部分,因为该评论尚未关闭,并且我希望它可以在子分类法周围使用另一个包装。