I have a xml and i want to read it's <block>
node. But when i execute below XPath , it's giving me an error.
The xml that i am trying to parse:
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)
below is the result saying:
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
I don't know what i am doing wrong? Can anyone guide me on this ?
There is nothing wrong with the code. The /block
XPath expression just selects nothing because the root element is an xml
element. Changing the expression to //block
selects those two block
elements. Example:
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With