XML

Converts between XML strings and JavaScript objects.

Where and why do we use the XML node?

The XML node processes Extensible Markup Language (XML) data. It converts between XML-formatted strings and JavaScript objects, making it essential when working with legacy systems, SOAP APIs, configuration files, or any service that uses XML for data exchange. This bidirectional conversion lets you parse incoming XML data for processing and format JavaScript objects into XML strings for output.

Modes of operation

The XML node operates in two directions depending on what it detects in the input:

XML String to Object

When the input is an XML string, the node parses it into a JavaScript object. This mode is essential when receiving data from SOAP APIs, reading XML configuration files, or processing XML payloads from devices and sensors. Once converted to an object, you can access and manipulate the data using standard JavaScript operations.

XML elements become object properties, nested elements create nested objects, and attributes are preserved in the conversion.

Object to XML String

When the input is a JavaScript object, the node converts it into an XML string. Use this mode when preparing data to send to SOAP services, writing XML configuration files, or transmitting structured data to systems that require XML format. The node generates valid, well-formed XML from your JavaScript object structure.

Property naming conventions

When converting between XML and an object, any XML attributes are added as a property named $ by default. Any text content is added as a property named _.

Understanding the $ property (attributes):

The $ property stores all XML attributes as key-value pairs.

<product id="P123" category="electronics" inStock="true">

Becomes:

{
product: {
$: {
id: "P123",
category: "electronics",
inStock: "true"
}
}
}

Understanding the _ property (text content):

The _ property stores the text content of an XML element.

<message>Hello World</message>
<price>29.99</price>

Becomes:

{
message: { _: "Hello World" },
price: { _: "29.99" }
}

Complex example combining both:

<user id="456" role="admin">
<username>jsmith</username>
<email verified="true">john@example.com</email>
<status>Active</status>
</user>

Becomes:

{
user: {
$: {
id: "456",
role: "admin"
},
username: { _: "jsmith" },
email: {
$: { verified: "true" },
_: "john@example.com"
},
status: { _: "Active" }
}
}

Demo Flow

XML String to Object

Object to XML String

Node Documentation

Converts between an XML string and its JavaScript object representation, in either direction.

Inputs

payloadobject | string
A JavaScript object or XML string.
options object
This optional property can be used to pass in any of the options supported by the underlying library used to convert to and from XML. See the xml2js docs for more information.

Outputs

payloadobject | string
  • If the input is a string it tries to parse it as XML and creates a JavaScript object.
  • If the input is a JavaScript object it tries to build an XML string.

Details

When converting between XML and an object, any XML attributes are added as a property named $ by default. Any text content is added as a property named _. These property names can be specified in the node configuration.

For example, the following XML will be converted as shown:

<p class="tag">Hello World</p>
{
  "p": {
    "$": {
      "class": "tag"
    },
    "_": "Hello World"
  }
}