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

  1. Pass the raw XML string or XML Node: prettyXml(response) when the response is XML.
  2. If the value is a Map/List of JSON data, use pretty() instead of prettyXml().
  3. 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

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


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)