Append the source xml file feature

Overview

The Append the source xml file feature on the tAdvancedFileOutputXML component is used to append records to an existing XML file. This feature also resolves the can't create multiple loop elements on tAdvancedFileOutputXML at a time limitation: you can output one entity to an XML file, and append another entity to the existing XML file with this feature. This article explains how to use this feature.

 

Environment

This procedure was written with:

  • Talend Open Studio for Data Integration 5.3.1.0-r104014
  • JDK version: Sun JDK build 1.6.0_26-b03
  • Operating system: Windows XP SP3

This procedure is compatible with Data Integration releases starting from 4.2.3.

 

Procedure

Requirement

Here are the two data sources used in this example:

person

Class_id Person_id Name
1 1 Shong
1 2 Elisa
2 1 Sabrina
2 2 Patrick

 

item

Class_id Item_id Item_name
1 1 item1
1 2 item2
2 1 item3

 

You need to read the data from the two data sources and generate an XML file as below:

<?xml version="1.0" encoding="ISO-8859-15"?>
<root>
  <class class_id="1">
    <person>
      <id>1</id>
      <name>Shong</name>
    </person>
    <person>
      <id>2</id>
      <name>Elisa</name>
    </person>
    <item>
      <item_id>1</item_id>
      <item_name>item1</item_name>
    </item>
    <item>
      <item_id>2</item_id>
      <item_name>item2</item_name>
    </item>
  </class>
  <class class_id="2">
    <person>
      <id>1</id>
      <name>Sabrina</name>
    </person>
    <person>
      <id>2</id>
      <name>Patrick</name>
    </person>
    <item>
      <item_id>1</item_id>
      <item_name>item3</item_name>
    </item>
  </class>
</root>

 

From the XML above, you can see that there are two loop elements in the file: person and item. However, the tAdvancedFileOutputXML component does not allow you to set multiple loop elements at a time. As a workaround, you can use the Append the source xml file feature of the tAdvancedFileOutputXML to avoid this restriction and solve the problem. For details about the implementation process, see the following section.

 

Create an example Job

  1. Use a tFixedFlowInput to generate the person data and link it to a tAdvancedFileOutputXML. The demo job is also available as an attachment.

    tFixedFlowInput_1.png

     

  2. In the File Name field of the tAdvancedFileOutputXML_1, enter the file path to the XML file to generate, for example: D:/out.xml.

    tAdvancedFileOutputXML_1.png

     

  3. Configure its XML tree as shown below. Set person as a loop element and set class as a group element.

    tAdvancedFileOutputXML_1_XMLTree.png

     

  4. Use a second tFixedFlowInput_2 to generate the item data, and link it to another tAdvancedFileOutputXML_2.

    tFixedFlowInput_2.png

     

  5. In the tAdvancedFileOutputXML_2, check the Append the source xml file box to append the data to an existing XML file, D:/out.xml.

    tAdvancedFileOutputXML_2.png

     

  6. Configure the XML tree as below, set item as a loop element and set class as a group element.

    tAdvancedFileOutputXML_2_XMLTree.png

     

    Warning: This feature requires the presence of a group element in the XML tree, for example, in this case the class element is a group element. Otherwise, with no group element, an error displays as below.

    No_group_element.png

 

Execute the Job

Execute the Job and see whether you get the expected result as shown in the Requirement section.

Version history
Revision #:
4 of 4
Last update:
‎06-29-2017 06:08 PM
Updated by:
 
Labels (1)
Tags (1)