Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluation of XPathResult is returning an error

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 ?

like image 500
Coderboi Avatar asked Jan 30 '20 12:01

Coderboi


1 Answers

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>
like image 89
Alejandro Avatar answered Oct 21 '22 19:10

Alejandro