{"record":{"id":"b090603f527e2548","repo":"karatelabs/karate","slug":"xmlpath-first-argument-must-be-xml-node-or-string-but-was","errorCode":null,"errorMessage":"xmlPath() first argument must be XML node or string, but was: {class}","messagePattern":"xmlPath\\(\\) first argument must be XML node or string, but was: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":974,"sourceCode":"\n    /**\n     * karate.xmlPath(xml, path) - Evaluate XPath on XML.\n     * First argument can be XML Node or String.\n     */\n    static JavaInvokable xmlPath() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"xmlPath() needs two arguments: xml and path\");\n            }\n            Object xmlObj = args[0];\n            String path = args[1].toString();\n            Node doc;\n            if (xmlObj instanceof Node) {\n                doc = (Node) xmlObj;\n            } else if (xmlObj instanceof String) {\n                doc = Xml.toXmlDoc((String) xmlObj);\n            } else {\n                throw new RuntimeException(\"xmlPath() first argument must be XML node or string, but was: \" + (xmlObj == null ? \"null\" : xmlObj.getClass()));\n            }\n            try {\n                return evalXmlPath(doc, path);\n            } catch (Exception e) {\n                throw new RuntimeException(\"xmlPath failed for path: \" + path + \" - \" + e.getMessage(), e);\n            }\n        };\n    }\n\n    // ========== Control Flow Utilities ==========\n\n    /**\n     * karate.fail(message) - Explicitly fail the scenario with a message.\n     */\n    static JavaInvokable fail() {\n        return args -> {\n            String message = args.length > 0 && args[0] != null ? args[0].toString() : \"karate.fail() called\";\n            throw new RuntimeException(message);","sourceCodeStart":956,"sourceCodeEnd":992,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L956-L992","documentation":"karate.xmlPath() accepts its XML source as a DOM Node or a String (which is parsed via Xml.toXmlDoc). Any other type — including null — cannot be evaluated, so the library throws this error, appending the value's class name (or 'null'). This is the source-type guard immediately before XPath evaluation.","triggerScenarios":"karate.xmlPath(someJsonObject, '/path') passing a Map/JSON instead of XML; passing null because an earlier request returned nothing; passing a number or byte array.","commonSituations":"Confusing a JSON response with an XML response in a test; XML variable not yet defined (null) at call time; passing an already-extracted value instead of the raw XML document.","solutions":["Pass the raw XML string or a DOM Node as the first argument: karate.xmlPath(xmlString, path).","If your data is JSON, use JSON path (karate's json handling) instead of xmlPath.","Guard against null: ensure the variable holding the XML is populated before the call.","The message tells you the offending class — use it to identify which wrong value leaked in."],"exampleFix":"// before\nkarate.xmlPath(response, '/order/id') // response is JSON\n// after\nkarate.xmlPath(responseXml, '/order/id')","handlingStrategy":"type-guard","validationCode":"if (xml == null) { throw new Error('xmlPath() source is null — check the earlier request/variable') }\nif (typeof xml !== 'string' && ('' + xml).charAt(0) !== '<') { karate.logger.warn('xmlPath() source does not look like XML') }","typeGuard":"function isXmlSource(v) { return v != null && (typeof v === 'string' || v.getClass && String(v.getClass()).indexOf('Node') >= 0 || (typeof v === 'object' && String(v).indexOf('#document') >= 0)) }","tryCatchPattern":"try { var val = karate.xmlPath(xml, path) } catch (e) { if (('' + e).indexOf('first argument must be XML node or string') >= 0) { karate.logger.warn('xmlPath source wrong type: ' + ('' + e)); val = null } throw e }","preventionTips":["Verify the response is XML (Content-Type or leading '<') before using xmlPath","Use JSON-path handling for JSON data instead of forcing it through xmlPath","Guard against null XML from failed requests before asserting on it","Parse strings to a DOM Node once and reuse the node for multiple path evaluations"],"tags":["javascript","argument-validation","xml","xpath","type-error"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}