karatelabs/karate · error · RuntimeException
prettyXml() argument must be XML node or string
Error message
prettyXml() argument must be XML node or string
What it means
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.
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().
Example fix
// before print prettyXml(responseJson) // after print pretty(responseJson) // or prettyXml(responseXmlString)
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof val == 'string' || val.nodeType) { print prettyXml(val) } else { print pretty(val) } Type guard
function isXml(v) { return typeof v == 'string' || (v && v.nodeType) } Prevention
- 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.
When it happens
Trigger: Calling prettyXml(myMap), prettyXml(jsonResponse), or prettyXml(someNumber) where the argument is neither a Node nor a String containing XML.
Common situations: 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.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
- prettyXml() needs one argument
- Dynamic expression must return a list or function
- read() needs at least one argument
- set() with a single argument expects a Map / JSON object
- cannot set xpath on non-XML variable:
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/7f8b4eff6f14b99b.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:444
result.add(i);
}
}
return result;
};
}
static JavaInvokable prettyXml() {
return args -> {
if (args.length == 0) {
throw new RuntimeException("prettyXml() needs one argument");
}
Object obj = args[0];
if (obj instanceof Node) {
return Xml.toString((Node) obj, true);
} else if (obj instanceof String) {
return Xml.toString(Xml.toXmlDoc((String) obj), true);
} else {
throw new RuntimeException("prettyXml() argument must be XML node or string");
}
};
}
static JavaInvokable repeat() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("repeat() needs two arguments: count and function");
}
int count = ((Number) args[0]).intValue();
JavaCallable fn = (JavaCallable) args[1];
List<Object> result = new ArrayList<>();
for (int i = 0; i < count; i++) {
result.add(fn.call(null, new Object[]{i}));
}
return result;
};
}View on GitHub (pinned to a22eb90246)