Skip to main content

XML Processing

XML is a flexible, text-based format used to store, transport, and structure data in a way that is both human-readable and machine-readable.

WSO2 Integrator provides built-in support for XML processing, making it easy to work with XML data in integration scenarios. You can create, read, query, modify, validate, and transform XML content without relying on external libraries. This native XML support simplifies integration development and helps efficiently process XML payloads exchanged between applications, services, and enterprise systems.

XML literals and construction

Create XML values directly in WSO2 Integrator using backtick templates. The xml type supports XML elements, text nodes, comments, and processing instructions, making it easy to construct structured XML payloads within integrations.

  1. Add a Variable: In the flow designer, click + and select Declare Variable. Set the variable type to xml and provide an XML backtick template as the expression (for example: xml <example></example>).

  2. Use embedded expressions: Insert dynamic values into XML templates using ${variableName} syntax. Each XML variable is represented as a separate Declare Variable step in the flow, allowing you to visually manage XML construction.

    Flow designer showing Declare Variable for XML literal construction including dynamic expressions

  3. Configure the expression: Select a variable node to view and edit the XML template expression from the side panel.

    Side panel showing the dynamic XML variable with embedded expression

XML text and comments

Create XML text nodes, comments, and processing instructions directly using XML backtick templates. These XML node types can be stored in variables and used when building or transforming XML payloads.

  1. Add Variable: In the flow designer, add separate Declare Variable with the type set to xml for each XML node type such as text, comment, or processing instruction.

  2. Define XML node values: Provide the required XML backtick template expression for the node type you want to create.

  3. View the flow representation: Each XML node appears as an individual Declare Variable step in the integration flow.

Flow designer showing Declare Variable for XML text, comment, and processing instruction nodes

Access child elements, attributes, and text content using XML navigation expressions in WSO2 Integrator. XML navigation makes it easy to query and extract specific parts of XML payloads during integration flows.

  1. Define the XML input: In the flow designer, click + and select Declare Variable. Set the type to xml and enter the XML literal as the expression. Name the variable catalog. This variable is then referenced in all subsequent navigation steps.

  2. Navigate child elements: Add a Declare Variable step and use expressions such as catalog/<product> to select child elements by name, or catalog/* to retrieve all child elements.

  3. Access text content: Use the .data() function in a variable expression (for example, (firstProduct/<name>).data()) to extract the text value of an XML element.

  4. Access attributes: Use .getAttributes()["attributeName"] to retrieve attribute values from an XML element.

  5. Filter descendants: Use descendant navigation expressions such as catalog/**/<name> to find matching elements at any level of the XML hierarchy.

    Flow designer showing Declare Variable for XML navigation including child access, text content, attributes, and descendant filtering

  6. View and edit expressions: Select a variable node to view or modify the XML navigation expression from the side panel.

    Side panel showing the products variable with catalog child access expression

XML namespaces

Handle namespaced XML using xmlns declarations in Ballerina.

info

xmlns namespace declarations cannot be added through the Visual Designer. Open the Ballerina source file directly and add the xmlns bindings at the top of the function or module before using namespace-prefixed expressions in the flow.

  1. Add namespace declarations in code: Open the .bal file and declare the required namespaces (for example, xmlns "http://example.com/orders" as ord;).

  2. Navigate namespaced elements: Add a Declare Variable and use the namespace prefix in the expression (for example, nsOrder/<cmn:customer>).

    Flow designer showing Declare Variable for namespaced XML construction and navigation

Iterating over XML

Use foreach loops or query expressions to process XML sequences in WSO2 Integrator. XML iteration is useful for reading, filtering, and transforming repeating XML elements such as lists of items, records, or orders.

  1. Add a Foreach step: Click + and select Foreach under Control. In the configuration panel, specify the XML collection to iterate over and the loop variable name.

    FieldDescription
    CollectionThe XML sequence to iterate over (for example, items/<item>)
    VariableThe loop variable bound to each XML element
  2. Process XML elements inside the loop: Add Declare Variable steps inside the loop body to extract values using XML navigation expressions such as (item/<sku>).data().

  3. Use query expressions for filtering: Add a Declare Variable step with a query expression to filter or transform XML sequences based on conditions.

    Flow designer showing a Foreach node iterating over XML items with variable extraction steps inside the loop body

XML mutation

Modify XML structures by updating child elements or attributes. XML mutation is useful when transforming payloads, enriching messages, or updating XML content dynamically during integration flows.

  1. Add a Variable: Create a Declare Variable with the type set to xml:Element and initialize it using an XML literal.

  2. Mutate the XML element: Click + and select Call Function. In the right-side panel, search for setChildren and select it from the lang.xml module. Provide doc as the target and the replacement XML literal as the argument.

    Right-side panel showing the setChildren function search result from the lang.xml module

    Flow designer showing a Declare Variable for the XML document followed by a Call Function step for setChildren

XML to record conversion

Use the data.xmldata module to convert XML data into typed Ballerina records for type-safe access and easier manipulation. Converting XML into records simplifies validation, transformation, and field access within integration flows.

  1. Define the target record types: Navigate to Types in the sidebar and click + to create type PurchaseOrder, ShipTo and Item, see Types.

    info

    The @xmldata:Attribute annotation marks a record field as an XML attribute. This annotation cannot be added through the Visual Designer. After creating the Item type, open the generated .bal file and add @xmldata:Attribute manually above the partNum field definition.

  2. Parse XML into the record type: In the flow designer, click + and select Call Function. In the right-side panel, search for parseAsType and select it from the data.xmldata module.

    right-side panel showing parseAsType search results with the data.xmldata module entry highlighted

    Provide the XML value product as the argument and set PurchaseOrder as the target record type.

    Flow designer showing the parseAsType function call step with PurchaseOrder as the result type

Record to XML conversion

Convert Ballerina records into XML using the data.xmldata module. Record-to-XML conversion is useful when generating XML payloads for APIs, external systems, or XML-based integrations.

  1. Define the record type: Navigate to Types in the sidebar and click + to create a new type using xml, see Types.

  2. Convert the record to XML: In the flow designer, click + and select Call Function. In the right-side panel, search for toXml and select it from the data.xmldata module. Provide the Invoice record inv as the argument and set the result type to xml.

right-side panel showing toXml search results with the data.xmldata module entry highlighted

  1. Use the generated XML: The resulting XML value can be returned from a service, sent to external systems, or further transformed within the integration flow.

Flow designer showing the toXml function call step with xml as the result type

XML to JSON conversion

Convert XML data to JSON by first parsing the XML into a typed record using the data.xmldata module, then converting the record to JSON using the built-in toJson() method from lang.value. To convert JSON back to XML, use xmldata:fromJson from data.xmldata. These conversions are useful when integrating XML-based systems with JSON-based APIs and services.

  1. Define the record types: Navigate to Types in the sidebar and click + to create each type from scratch. Define the source record with fields matching your XML structure and the target record with fields required for the JSON output. For more information, see Types.

  2. Parse XML into a record: Follow the steps in XML to record conversion to parse the XML value into a typed record.

  3. Convert the record to JSON: Click + and select Call Function. In the right-side panel, search for toJson and select it from the lang.value module. Provide the parsed record as the argument. The return value is a json value.

    Flow designer showing the XML parse, mapOrder, and toJson return steps in sequence

  4. Map fields visually: Use the Visual Data Mapper to map or transform fields between record structures before converting the result into JSON.

What's next