How to process XML

Introduction

This page explains how to absorb XML data. Absorbing data consists in inserting data into a deployment using the route component tnd-absorb.

For this purpose, you can follow along with an example on how to import information about books. We assume that the deployment already contains an entity for Books, and an entity for Authors. The entity key for Books is pk_book, and the entity key for Authors is pk_author.

An example books.xml file is provided below for your convenience.

books.xml
<?xml version="1.0"?>
<catalog>
   <!-- This XML sample file comes from Microsoft.                                     -->
   <!-- They are using it for pretty much all their XML code examples.                 -->
   <!-- http://msdn.microsoft.com/en-us/library/windows/desktop/ms762271(v=vs.85).aspx -->
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="bk109">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="bk110">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="bk111">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

The exercise

The books.xml file contains information about various books. For this exercise, you are going to import published books along with the following information for each book:

  • title
  • author (as relation towards an Author entity)
  • price (as a decimal value)

We are using a simple model where Book and Author are entities.

Create resources

First, start by creating a Books resource.

Step Action
1 On the main menu, click the cog icon > Data Integration.
2 On the left menu, click Resources.
3 Click New Resource.
4 In the Name field, enter Books.
5 Leave the default options as is for the Type and Charset fields.
6 In the Content text box, paste the content of the books.xml file provided in this page.

Create the mapping

To bring in the books and the data you want, create a mapping that tells the deployment  which data to bring import and into which format.

Create parameters

Step Action
1 To access the Mappings screen, from Data integration, click Mappings on the left menu.
2 Click New Mapping.
3 In the name field, enter InsertBook.
4 Click the Add parameter hyperlink.
5 In the Name field, enter title. In the Type drop-down, select String. Click Done.
6

Repeat steps 4-5 to create the following parameters:

  1. author parameter with String type
  2. price parameter with Decimal type
  3. publish date parameter with Instant type

Your screen should look like this:

Add instance operations

The following screenshot displays how to configure the operation for your mapping.

The following provides information about the areas of the screen:

  1. The publish date is the:
    1. resolution time: with the auto-create flag it will be the creation date of the instances
    2. operation begin: in order to absorb the fields at the creation of the entity
  2. This parameter auto-creates books.
  3. This parameter auto-creates the author for each book.
  4. This parameter sets the price.

To create the instance operation, follow these steps:

Step Action
1 Click the Add instance operation button.
2 Click the select entity hyperlink. A Select entities pop-up is displayed.
3 Select the Book entity, then click Done.
4 In the Default Times area, set the Resolution drop-down to Parameter in the first drop-down, then publish date in the second one.
5 Set the Operations begin drop-down to Constant in the first drop-down, then publish_date in the second one.
6 In the Resolution area, the pk_book key is automatically populated. In the drop-down next to pk_book, select the Create an instance of Book if not found at resolution time option.
7 On the line below for the value of title, in the first drop-down, select Parameter, and in the second parameter, select Title.
8 In the Operations area, click the drop-down and select the Change value of attribute option.
9 Click the author attribute, then click Select under Actions. An operation set for author is created.
10 In the set for the author parameter, for the Key settings, from the drop-down, select the Create an instance of Author if not found option.
11 For the Value setting for this set, in the first drop-down, leave Parameter, and in the second drop-down, select author.
12 Create another operation set for price. Click the drop-down at the top of the Operations area, and select the Change value(s) of attribute option.
13 In the price operation set, in the line for Attribute, in the first drop-down, leave Parameter as is, and in the second drop-down, select price.
14 Click Save.


Route

Sample route definition

Create a route using the following route definition.

Books
<routes xmlns="http://camel.apache.org/schema/spring" xmlns:u="http://www.systar.com/aluminium/camel-util">
    <route>
        <!-- URI for manual injection of the XML -->
        <from uri="direct:book-injection"/>
        <!-- Convert as XML -->
        <convertBodyTo type="org.w3c.dom.Document"/>
        <split>
            <!-- For each book of the catalog -->
            <xpath>/catalog/book</xpath>
            <!-- Extract the book data from XML to the absorption parameter map -->
            <setBody>
                <u:map-create>
                    <u:map-entry key="title">
                        <xpath resultType="java.lang.String">/book/title/text()</xpath>
                    </u:map-entry>
                    <u:map-entry key="author">
                        <xpath resultType="java.lang.String">/book/author/text()</xpath>
                    </u:map-entry>
                    <u:map-entry key="price">
                        <xpath resultType="java.lang.String">/book/price/text()</xpath>
                    </u:map-entry>
                    <u:map-entry key="publish_date">
                        <u:date-parse pattern="yyyy-MM-dd">
                            <xpath resultType="java.lang.String">/book/publish_date/text()</xpath>
                        </u:date-parse>
                    </u:map-entry>
                </u:map-create>
            </setBody>
            <!-- Absorb the data -->
            <to uri="tnd-absorption:InsertBook"/>
        </split>
    </route>

Create the route for data absorption

To create a route, follow these steps.

Step Action
1 From the Application data integration screen, click  Routes on the left menu. 
2 Click  New Routing Context
3 In the  Name field, enter  CreateBooks.
4 Paste the route definition provided above into the text box for the route.
5 In the Actions column for the new route, click the play icon to start your route.

Injection

Once the route is started, you can inject data. Use the manual injection method for the books.xml file since the way we retrieve the XML data in the first place is not really important. See documentation page.

Application

Here is the export of this demo application: XMLSample.appx


Related Links