Using the XML parser to parse an XML string into an XML object
You can use the XML parser to transform a string of XML text in UTF-8 encoding into an XML object representation of the string.The XML parser generates an output schema for the XML object, displayed in the flow editor as a tree structure that shows each element with its data type, as well as any attributes..
When creating event-driven or API flows in IBM® App Connect, you might sometimes need to pass XML content that's stored in one application into another application as input. App Connect provides a built-in XML parser that you can use to map XML data from an event or action to a subsequent node in a flow. The XML parser transforms a string of XML text in UTF-8 encoding into an XML object representation of the string.
The parser supports versions 1.0 and 1.1 of the XML specification.
The XML parser is available from the Toolbox tab in the flow editor.
Defining the data to be parsed and generating valid XML schema
We'll use an example to describe how the XML parser works. Let's suppose that you want create a flow that adds new product information from suppliers into your database management system. The product information is in plain-text XML format, and is uploaded as an XML file to your SFTP server for processing.
After setting up your flow to retrieve the contents of an XML file that was uploaded to your SFTP server, you need to insert an XML parser node from the App Connect toolbox. You can use this node to define the data that you want to parse and to generate valid XML schema. To insert and configure the XML parser node, complete the following steps:
- Click the (+), go to the Toolbox tab, and then click XML
parser.
Figure 1. Selecting the XML parser from the Toolbox tab (Click image to view full size.)
- Use the XML Input field to specify which data from the previous action or
event should be parsed. You can map to fields that are shown in the list of available inputs
(generally accessible by clicking Insert a mapping
). Typically, this would be the contents of an XML file or some other XML content in a body of text. In our example, we are mapping to the contents of a file in the SFTP server.
Figure 2. XML Input entry showing a mapped field (Click image to view full size.)
- Expand the Output Schema section and click Generate an XML
schema.
Figure 3. Generate an XML schema link (Click image to view full size.)
- In the "Generate an XML Schema" panel, use the Sample XML field to specify an XML
representation of the content that you want to parse. In our example, the sample XML uses the
following elements to describe our product data: a root element (
productinfo
), aproduct
child element with acategory
attribute, and nestedproductid
,name
,description
,price
, andmanufacturer
siblings.Figure 4. Sample XML field (Click image to view full size.)
Consideration for completing the Sample XML field:
- The Sample XML field accepts a maximum of 16,000 characters.
- To convert the sample XML into an XML object, click Generate schema. The
generated schema is displayed as a tree structure that shows each element with its data type, as
well as any attributes.
Figure 5. Generated XML schema tree in an XML parser node (Click image to view full size.)
Tip: To modify the schema, you can click Regenerate schema to reopen the Generate an XML Schema panel and specify a different XML sample.
Ways of processing your parsed data
The output from an XML parser node is an array. So, to use this output in a subsequent node in the flow, you'd typically include a For each node after the XML parser node to iterate through the array, or you could manipulate the array data by using JSONata.