- Index

< Previous |  | Next >

# XML to Tuple and Tuple to XML Operator Sample

## About This Sample

This sample shows how to use the TIBCO StreamBase® XML to Tuple and Tuple to XML operators.

The XML to Tuple Java operator converts XML-encoded messages to StreamBase tuples. The operator's input port schema has a single string field that passes an XML-encoded message to the operator. The operator parses the XML message and populates tuple fields corresponding to the elements and attributes found in the message. Each XML message enqueued to the operator results in a single tuple emitted on its output port.

The Tuple to XML Java operator converts StreamBase tuples to XML-encoded messages. Each field in the operator's input port schema populates an element or attribute in the resulting XML message. Each tuple enqueued to the operator results in a single XML message emitted on its output port.

See the description of the operators' Properties views in Using the XML to Tuple Operator and Using the Tuple to XML Operator.

Several sample applications illustrate the use of the XML to Tuple operator. In this first set of samples, the operator parses an XML message and populates the corresponding fields in the output tuple. Each sample application includes a file containing a canned XML message to obviate entering XML strings manually.

- The `xml2tuple-datatypes.sbapp` sample illustrates populating tuple fields from XML elements for a variety of StreamBase data types, including primitive types, lists, and nested tuples. XML attributes are disabled and therefore discarded by the operator.

- The `xml2tuple-attributes.sbapp` sample illustrates two techniques for retrieving XML attributes. The sample's output schema is configured to receive attributes for the following fields:

  - Top

  - Top.MyInt

  - Top.MyString

  - Top.MyStringList

- The `xml2tuple-fixml.sbapp` sample shows how the operator handles a more complex, hierarchical XML input string that includes element and attribute values, using a FIXML message for its example input.

- The `xml2tuple-xpath.sbapp` sample shows how the operator handles XPath operations to produce sub XML chunks, single elements or lists of data based on the XPath expression.

- The `xslt.sbapp` sample shows how you can use a pre/post process XSLT to convert XML into a more suitable format for input into the xml2tuple operator or as output from the tuple2xml operator.

Two sample applications illustrate the use of the Tuple to XML operator. In the second set of samples, the operator generates and emits an XML message based on the contents of an input tuple.

- In `tuple2xml-datatypes.sbapp`, XML attributes are disabled and are therefore not included in the generated XML message.

- In `tuple2xml-attributes.sbapp`, XML attributes are enabled, and the sample's input schema is configured to generate attributes for the following fields:

  - Top

  - Top.Middle

## Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

- From the top-level menu, click File → Load StreamBase Sample.

- Enter `xml` to narrow the list of options.

- Select xmltuple from the Data Constructs and Operators category.

- Click OK.

StreamBase Studio creates a single project for the operator samples.

## Running the XML to Tuple Samples in StreamBase Studio

1. In the Project Explorer view, open the sample you just loaded.

   If you see red marks on a project folder, wait a moment for the project to load its features.

   If the red marks do not resolve themselves after a minute, select the project, right-click, and select Maven → Update Project from the context menu.

2. Open the `src/main/eventflow/`packageName`` folder.

3. Open the ``xml2tuple-datatypes.sbapp`` file and click the Run button. This opens the SB Test/Debug perspective and starts the module.

4. In the Manual Input view, select the `TupleIn` stream and click Send Data.

5. In the Output Streams view, observe a tuple dequeued on the `TupleOut` stream in which fields are populated from the contents of the XML message contained in `xml2tuple-datatypes.xml`, located in `src/main/resources`.

6. Experiment by entering other values in the `XML` field of the `XMLIn` stream, clicking Send Data, and observing the resulting output tuples.

7. When done, press F9 or click the Terminate EventFlow Fragment button.

8. Next, open and run the `xml2tuple-attributes.sbapp` application.

9. In the Manual Input view, select the `TupleIn` stream and click Send Data.

10. In the Output Streams view, observe tuples emitted on the `TupleOut1` and `TupleOut2` streams in which fields are populated from the contents of the XML message contained in `xml2tuple-attributes.xml`, located in `src/main/resources`. The attributes in the `TupleOut1` tuple appear in subfields whose names match the corresponding XML attribute names, while the attributes in the `TupleOut2` tuple appear in subfields of fields named `_ATTRIBUTES`.

11. Experiment by entering other values in the `XML` field if the `XMLIn` stream, clicking Send Data, and observing the resulting output tuples.

12. When done, press F9 or click the Terminate EventFlow Fragment button.

13. Next, open and run the `xml2tuple-fixml.sbapp` application.

14. In the Manual Input view, select the `StartRead` stream and click Send Data.

15. This sample's FIXML input string is provided for you via the `xml2tuple-fixml.xml` file, located in `src/main/resources`.

16. Notice a single tuple emitted on the `TupleOut` stream. Open the tuple's hierarchical levels by clicking the triangles in the Field column.

17. When done, press F9 or click the Terminate EventFlow Fragment button.

18. Next, open and run the `xml2tuple-xpath.sbapp` application.

19. In the Manual Input view, select the `ReadFile` stream and click Send Data.

20. This sample's XML input string is provided for you via the ``xml2tuple-xpath.xml`` file, located in `src/main/resources`.

21. Notice a single tuple emitted on the `XPathOut` stream. Open the tuple's hierarchical levels by clicking the triangles in the Field column. Also notice that if a non-list schema type is provided and the XPath expression produces a list of data, only the last XPath node will be used to populate the schema element. If you want all elements then use the list data type.

22. When done, press F9 or click the Terminate EventFlow Fragment button.

23. Next, open and run the `xslt.sbapp` application.

24. In the Manual Input view, select the `TransactionsToParse` or `TransactionsToRender` stream and click Send Data.

25. This sample's contains defaults in the map operations after the input streams. You can inspect those map operators to see the input values into each operator.

26. The parsing operation uses the `sb-xslt-parse.xsl` file to convert the input payload XML to a format that the xml2tuple operator can understand.

27. The tuple-to-XML operation uses the `sb-xslt-render.xsl` file to convert the XML produced by converting the tuple into a more friendly output to third party systems.

28. After sending tuples into each of the input streams, you can inspect the output to view how the default values were converted to and from tuples and XML. Note that the operators have debug logging enabled and will output the intermediate XML between the tuple, XML, and XSLT converted XML.

29. When done, press F9 or click the Terminate EventFlow Fragment button.

## Running the Tuple to XML Samples in StreamBase Studio

1. Open and run the `tuple2xml-datatypes.sbapp` application.

2. In the Manual Input view, select the `TupleIn` stream, enter `123` in the `Top.MyInt` field and click Send Data.

3. In the Output Streams view, observe a single tuple emitted on the `XMLOut` stream in which the `XML` field contains `<Top><MyInt>123</MyInt><MyTuple></MyTuple></Top>`.

4. Experiment by entering other values in various other fields in the Manual Input view, clicking Send Data, and observing the resulting output tuples.

5. When done, press F9 or click the Terminate EventFlow Fragment button.

6. Now run the `tuple2xml-attributes.sbapp` application.

7. In the Manual Input view, select the `TupleIn` stream, enter `myattrvalue1` and `myattrvalue2` in the `Top.s1` and `Top.Middle[0].s2` fields, respectively, and click Send Data.

8. In the Output Streams view, observe a single tuple from the `XMLOut1` stream in which the `XML` field contains `<Top s1="myattrvalue1"><Middle s2="myattrvalue2"></Middle></Top>`.

9. When done, press F9 or click the Terminate EventFlow Fragment button.

## Sample Location

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace, which is normally part of your home directory, with full access rights.

### Important

Load this sample in StreamBase Studio, and thereafter use the Studio workspace copy of the sample to run and test it, even when running from the command prompt.

Using the workspace copy of the sample avoids permission problems. The default workspace location for this sample is:

    studio-workspace/sample_xmltuple

See Default Installation Directories for the default location of `studio-workspace` on your system.