gpt4 book ai didi

javascript - 在 Javascript 中调用包含 promise 的递归函数

转载 作者:行者123 更新时间:2023-11-30 06:22:52 26 4
gpt4 key购买 nike

我正在尝试通过解析 xml 文件来构建一个 json 树。这些文件可能包含对其他 xml 文件的引用。我要解析的所有文件的名称都类似于 toc\d.js .输出的树应具有以下形式:

{
name: 'name of element',
url: 'xml_referenced.xml',
children: [
{
name: '.....',
url: '.....',
children: [...]
}
}

应该生成的 xml 可能如下所示 (toc.xml)

<?xml version=\"1.0\" encoding=\"utf-8\" ?><data  src=\"toc.js\"  name=\"Using and Customizing the Application\" url=\"DA_UsingAndCustomizing.htm\"><item name=\"Adapted user interface\" url=\"DA_AdaptedUserInterface.htm\" /><item name=\"Show or hide the windows\" url=\"3402556939.htm\" /><book  src=\"toc2.js\"  name=\"Work with layouts\" url=\"9007202657330059.htm\" /><book  src=\"toc3.js\"  name=\"Adjust table views\" url=\"3402653835.htm\" /><item name=\"Use the keyboard to access the ribbon\" url=\"9007202657380875.htm\" /><item name=\"Keyboard shortcuts\" url=\"27021601196225675.htm\" /><item name=\"Lock or unlock the Data Analysis session\" url=\"27021601166795787.htm\" /><item name=\"Reset all user settings\" url=\"3402736267.htm\" /><item name=\"Find status information\" url=\"9007203112007179.htm\" /><item name=\"Navigation pane\" url=\"18014401941480331.htm\" /><item name=\"PDF Viewer\" url=\"OL_PDFViewer.htm\" /><item name=\"Review mode\" url=\"DA_ReviewMode.htm\" /><item name=\"Customize reports and results\" url=\"DA_CustomizeReportsAndResults.htm\" /><book  src=\"toc4.js\"  name=\"Interfaces\" url=\"DA_Interfaces.htm\" /></data>"

如您所见,它包含引用更多“toc”文件的元素(将转换为 xml,因为它们存储为 js):

<book  src=\"toc2.js\"  name=\"Work with layouts\" url=\"9007202657330059.htm\" />

我用来解析的函数如下:

var loadedPaths = []
var buildTOC = function(xml, srcPath){
const parseToc = function(toc){
var obj = {}
var children
if (toc.children.length){
children = toc.children // THESE ITEMS ARE INCLUDED IN THE RESULT
}
else {
children = []
}
var path = toc.attribs.src
if (path && loadedPaths.indexOf(path)<0){
loadedPaths.push(path)
lib.getXml(srcPath + '/' + toc.attribs.src).then(x => { // RETURNS XML
children = lib.buildTOC(x, srcPath) // THESE ITEMS ARE NOT INCLUDED

})
}
else {
obj.url = toc.attribs.url
obj.name = toc.attribs.name
obj.children = children.map(x => {return parseToc(x)})
}
return obj
}
var $ = this.buildDom(xml, {xmlMode: true}) // RETURNS A CHEERIO DOM
console.log([parseToc($('data')[0])])
return [parseToc($('data')[0])]


}

结果只包括<item><book>原始 toc.xml 文件中的元素。我期待的是 <book>元素还包括子元素,它们是 <data> 的子元素在 toc2.js、toc3.js 等文件中标记

谁能帮我弄清楚这里出了什么问题?谢谢。

最佳答案

下面是如何使用 promises 做这种事情:

var buildTOC = function(xml, srcPath){
const parseToc = function(toc){
const children = ...;//create the children url list here
const childrenToc = children.length===0 ? Promise.resolve([]) : Promise.all(children.map(childUrl => load(childUrl))).then(childXml => buildTOC(childXml));
//here, load would be your lib.getXml function
return childrenToc.then(childrenTocsList => ({
url: toc.attribs.url,
name: toc.attribs.name,
children: childrenTocsList
}));
}
var $ = this.buildDom(xml, {xmlMode: true}) // RETURNS A CHEERIO DOM
return parseToc($('data')[0]);
}

//Usage:
buildTOC(myXML, mySrcPath).then(result => console.log(result));

您只需创建children 列表和load 函数即可满足您的需求。

关于javascript - 在 Javascript 中调用包含 promise 的递归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52163906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com