karatelabs/karate · error · RuntimeException
setXml() needs at least two arguments: name and xml
Error message
setXml() needs at least two arguments: name and xml
What it means
karate.setXml(name, xml) parses an XML string into a document node and assigns it to the named variable; an optional third argument adds a path/value operation. The library throws this error when fewer than two arguments are given, since both the variable name and the XML content are required.
Solutions
- Pass both arguments: karate.setXml('myXml', '<root><a>1</a></root>').
- If the XML is derived from another variable, coerce and verify it first: karate.setXml('myXml', String(xmlString)).
- If assigning a non-XML value, use karate.set(name, value) instead.
- Optionally pass a third argument for path-based updates: karate.setXml('myXml', '<x/>', '/x/a', '1').
Example fix
// before
karate.setXml('myXml')
// after
karate.setXml('myXml', '<root><item>value</item></root>') Defensive patterns
Strategy: validation
Validate before calling
// JS, before calling
if (typeof xmlString !== 'string' || xmlString.trim().length === 0) {
throw new Error('setXml() requires a non-empty XML string');
}
karate.setXml('myXml', xmlString); Type guard
function isNonEmptyXml(s) { return typeof s === 'string' && s.trim().startsWith('<'); } Try / catch
try {
karate.setXml('myXml', xmlString);
} catch (e) {
if (String(e.message).indexOf('setXml() needs at least two arguments') !== -1) {
throw new Error('setXml() missing name or xml — xml resolved to: ' + xmlString);
}
throw e;
} Prevention
- Never omit the xml argument even when the value is dynamically generated.
- Use karate.set for non-XML values to avoid arity/format confusion.
- When XML comes from a response, extract and stringify it before assignment.
When it happens
Trigger: Calling karate.setXml(name) with no XML argument, or with none at all — commonly when the XML string comes from an undefined variable, or when confusing setXml with set and omitting the value.
Common situations: Building XML dynamically from a response where the extracted value is undefined; migrating karate.set calls to setXml without updating arity; templated XML that interpolated to empty/undefined.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- remove() needs two arguments: variable name and path
- prettyXml() needs one argument
- eval() needs one argument
- expect() needs at least one argument
- exec() needs at least one argument
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/20e5f49244872dec.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:781
Object var = engine.get(varName);
if (var instanceof Node && path != null && path.startsWith("/")) {
// XPath remove on XML
Document doc = var instanceof Document ? (Document) var : ((Node) var).getOwnerDocument();
Xml.removeByPath(doc, path);
} else if ((var instanceof Map || var instanceof List) && path != null) {
// Route through Json so nested paths (props.field3), JsonPath ($.props.field3)
// and bracketed keys all resolve - mirroring the `remove` keyword. A bare
// top-level key is normalized to a JsonPath by Json.prefix().
Json.of(var).remove(path);
}
return null;
};
}
private JavaInvokable setXml() {
return args -> {
if (args.length < 2) {
throw new RuntimeException("setXml() needs at least two arguments: name and xml");
}
String name = args[0].toString();
if (args.length == 2) {
// Simple form: setXml('name', '<xml/>')
String xml = args[1].toString();
engine.put(name, Xml.toXmlDoc(xml));
} else {
// Path form: setXml('name', '/path', '<xml/>')
String path = args[1].toString();
String xml = args[2].toString();
Object target = engine.get(name);
if (target instanceof Node) {
Node doc = (Node) target;
if (doc.getNodeType() != Node.DOCUMENT_NODE) {
doc = doc.getOwnerDocument();
}
Xml.setByPath((org.w3c.dom.Document) doc, path, Xml.toXmlDoc(xml));
}View on GitHub (pinned to a22eb90246)