javascript - Evaluation of XPathResult is returning an error - Stack Overflow

I have a xml and i want to read it's <block> node. But when i execute below XPath , it'

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 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 ?

Share Improve this question edited Jul 24, 2020 at 6:00 0xAnon asked Jan 30, 2020 at 12:56 0xAnon0xAnon 89710 silver badges24 bronze badges 1
  • There are two <block> nodes in your xml, one the child of the other; which one is your target? And what do you expect the value of r to be? – Jack Fleeting Commented Jan 30, 2020 at 13:37
Add a ment  | 

2 Answers 2

Reset to default 5

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>

Just for future reference, the return value MUST be string serializable. Note I return always a JSON object and i use the console.log to check why it failed. The console is the one of the browser.

            const responses = await page.evaluate(
                async () => {
                    try {
                        const response = '//*/div/descendant-or-self::text()';
                        const paragraphs = document.evaluate(response, document);
                        let responses = [];
                        let p = paragraphs.iterateNext();
                        while (p !== null) {
                            responses.push(p.textContent);
                            p = paragraphs.iterateNext();
                        }
                        // can return only things that can be stringified
                        return {
                            dialog: responses
                        };
                    } catch (err) {
                        console.log(err);
                        return {
                            message: err
                        };
                    }
                }
            );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744929314a4601634.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信