{"record":{"id":"7f8b4eff6f14b99b","repo":"karatelabs/karate","slug":"prettyxml-argument-must-be-xml-node-or-string","errorCode":null,"errorMessage":"prettyXml() argument must be XML node or string","messagePattern":"prettyXml\\(\\) argument must be XML node or string","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":444,"sourceCode":"                    result.add(i);\n                }\n            }\n            return result;\n        };\n    }\n\n    static JavaInvokable prettyXml() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"prettyXml() needs one argument\");\n            }\n            Object obj = args[0];\n            if (obj instanceof Node) {\n                return Xml.toString((Node) obj, true);\n            } else if (obj instanceof String) {\n                return Xml.toString(Xml.toXmlDoc((String) obj), true);\n            } else {\n                throw new RuntimeException(\"prettyXml() argument must be XML node or string\");\n            }\n        };\n    }\n\n    static JavaInvokable repeat() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"repeat() needs two arguments: count and function\");\n            }\n            int count = ((Number) args[0]).intValue();\n            JavaCallable fn = (JavaCallable) args[1];\n            List<Object> result = new ArrayList<>();\n            for (int i = 0; i < count; i++) {\n                result.add(fn.call(null, new Object[]{i}));\n            }\n            return result;\n        };\n    }","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L426-L462","documentation":"prettyXml() accepts only an org.w3c.dom Node or an XML String; any other type (Map, List, number, etc.) reaches the else branch and throws. The library cannot safely convert arbitrary objects to XML, so it fails fast with this message.","triggerScenarios":"Calling prettyXml(myMap), prettyXml(jsonResponse), or prettyXml(someNumber) where the argument is neither a Node nor a String containing XML.","commonSituations":"Passing a parsed JSON response to an XML helper; calling prettyXml on a variable that was converted to a Map earlier in the script; a REST endpoint returned JSON but the test assumed XML.","solutions":["Pass the raw XML string or XML Node: prettyXml(response) when the response is XML.","If the value is a Map/List of JSON data, use pretty() instead of prettyXml().","If you have a Java object, serialize it to an XML string first, then call prettyXml()."],"exampleFix":"// before\nprint prettyXml(responseJson)\n// after\nprint pretty(responseJson) // or prettyXml(responseXmlString)","handlingStrategy":"type-guard","validationCode":"if (typeof val == 'string' || val.nodeType) { print prettyXml(val) } else { print pretty(val) }","typeGuard":"function isXml(v) { return typeof v == 'string' || (v && v.nodeType) }","tryCatchPattern":null,"preventionTips":["Route JSON data to pretty() and XML data to prettyXml().","Confirm the API response format matches the test's assumption (JSON vs XML).","Do not pass Maps/Lists/numbers to prettyXml(); serialize to an XML string first if needed."],"tags":["karate","javascript","type-mismatch","xml"],"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"}