gpt4 book ai didi

javascript - XPathResult 的评估返回错误

转载 作者:行者123 更新时间:2023-12-03 07:05:11 29 4
gpt4 key购买 nike

我有一个 xml,我想阅读它的 <block>节点。但是当我在 XPath 下面执行时,它给了我一个错误。
我试图解析的 xml:

   let x = `<xml>
<block type="block_onGreenFlagClicked" id=";]jZ*Zs|[L-Sr{nhCB%V" x="25" y="0">
<field name="NAME1">When</field>
<statement name="function_name">
<block type="helper_Player__walkSecondsDrop" id="9lo_/{gKTxB6/FRH2Pw">
<field name="LABEL">Player.</field>
<field name="DROP_Player__walkSecondsDrop$">walkForwardForNSeconds</field>
</block>
</statement>
</block>
</xml>`;



let doms = new DOMParser()
let v = doms.parseFromString(x, 'text/xml')
let r = v.evaluate('/block',v, null, XPathResult.ANY_TYPE, null)
下面是结果:

XPathResult {invalidIteratorState: false, resultType: 4, iterateNext: function, snapshotItem: function, ANY_TYPE: 0…}
booleanValue: [Exception: TypeError: Failed to read the 'booleanValue' property from 'XPathResult': The result type is not a boolean.]
invalidIteratorState: false
numberValue: [Exception: TypeError: Failed to read the 'numberValue' property from 'XPathResult': The result type is not a number.]
resultType: 4
singleNodeValue: [Exception: TypeError: Failed to read the 'singleNodeValue' property from 'XPathResult': The result type is not a single node.]
snapshotLength: [Exception: TypeError: Failed to read the 'snapshotLength' property from 'XPathResult': The result type is not a snapshot.]
stringValue: [Exception: TypeError: Failed to read the 'stringValue' property from 'XPathResult': The result type is not a string.]
__proto__: XPathResult

我不知道我做错了什么?任何人都可以指导我吗?

最佳答案

代码没有问题。 /block XPath 表达式什么都不选择,因为根元素是 xml元素。将表达式更改为 //block选择那两个block元素。例子:

let x = `<xml>
<block type="block_onGreenFlagClicked" id=";]jZ*Zs|[L-Sr{nhCB%V" x="25" y="0">
<field name="NAME1">When</field>
<statement name="function_name">
<block type="helper_Player__walkSecondsDrop" id="9lo_/{gKTxB6/FRH2Pw">
<field name="LABEL">Player.</field>
<field name="DROP_Player__walkSecondsDrop$">walkForwardForNSeconds</field>
</block>
</statement>
</block>
</xml>`
let doms = new DOMParser()
let v = doms.parseFromString(x, 'text/xml')
let r = v.evaluate('//block', v, null, XPathResult.ANY_TYPE, null)

var s = new XMLSerializer();
var t = document.getElementById("output");
while ((n = r.iterateNext())) {
t.value += (s.serializeToString(n)) + "\n";
}
<body>
<textarea id="output" rows="15" cols="60"></textarea>
</body>

关于javascript - XPathResult 的评估返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59986563/

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