Appending records to an existing XML file - 7.1

author
Shicong Hong
EnrichVersion
7.1
EnrichProdName
Talend Big Data
Talend Big Data Platform
Talend Data Fabric
Talend Data Integration
Talend Data Management Platform
Talend Data Services Platform
Talend ESB
Talend MDM Platform
Talend Open Studio for Big Data
Talend Open Studio for Data Integration
Talend Open Studio for ESB
Talend Open Studio for MDM
Talend Real-Time Big Data Platform
task
Data Governance > Third-party systems > XML components
Data Quality and Preparation > Third-party systems > XML components
Design and Development > Third-party systems > XML components
EnrichPlatform
Talend Studio

The Append the source XML file feature

This scenario shows the way to append records to an existing XML file.

The Append the source xml file feature provided by the tAdvancedFileOutputXML component appends 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 first and append another entity to the existing XML file with this feature.

This scenario generates an XML file using the data in the following two tables.

Person

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

Item

Class_id Person_id Name
1 1 item1
1 2 item2
2 1 item3

The scenario reads the above data and generates the following XML file.

Tip: From the above XML file, you can see that there are two loop elements in the file: person and item. However, the tAdvancedFileOutputXML component does not support setting multiple loop elements at a time. You can use the Append the source xml file feature of the tAdvancedFileOutputXML to avoid this restriction and solve the problem.

Setting up the Job for generating an XML file

This Job uses the following components.

  • two tFixedFlowInput components to generate the person and the item data from a text file;
  • two tAdvancedFileOutpuXML components to generate an XML file and append records to the XML file.

Procedure

  1. Drop a tFixedFlowInput and a tAdvancedFileOutputXML (named as tFixedFlwInput_1 and tAdvancedFileOutputXML_1 by default) from the Palette onto the design workspace. Connect the two components using a Row > Main link.
  2. Double-click tFixedFlowInput_1 to display its Basic settings view and select the Use Inline Content (delimited file). In the Content field, add the following information.
    1;1;Shong
    1;2;Elisa
    2;1;Sabrina
    2;2;Patrick
  3. Click the Edit Schema button and create the three columns shown in the following figure.
  4. When prompted, click OK to propagate the schema to the subsequent component (that is, the tAdvancedFileOutputXML_1 component). You can also achieve this by clicking the Sync columns button in the Basic settings view of the tAdvancedFileOutputXML_1 component.
  5. In the Basic settings view of the tAdvancedFileOutputXML_1 component, click the Configure XML tree button to open the XML tree editor (as shown below). We need to set person as a loop element and set class as a group element.
    Note: This feature requires the presence of a group element in the XML tree, otherwise an error will be prompted below the name of the component in this window.
  6. Click root and then, click the plus button to add the sub-element class by selecting Create as sub-element.
  7. Repeat the procedure to add the sub-element person to class and to add the sub-elements id and name to person.
  8. Right-click class to open its contextual menu and click Add attribute to add the attribute class_id.
  9. Click the Auto map! button to link the input schema to the XML Tree
  10. In the File Name field of the Basic settings view, enter the file path to the XML file to generate, for example: "D:/out.xml".

Appending records to the XML file generated

Procedure

  1. Drop a second tFixedFlowInput and a second tAdvancedFileOutputXML (named as tFixedFlwInput_2 and tAdvancedFileOutputXML_2 by default) from the palette onto the design workspace. Connect the two components using a Row > Main link.
  2. Connect tFixedFlowInput_1 to tFixedFlowInput_2 with a Trigger > OnSubjobOk link.
  3. Double-click tFixedFlowInput_2 to display its Basic settings view and select the Use Inline Content (delimited file). In the Content field, add the following.
    1;1;item1
    1;2;item2
    2;1;item3
  4. Click the Edit Schema button and create the three columns shown in the following figure.
  5. When prompted, click OK to propagate the schema to the subsequent component (that is, the tAdvancedFileOutputXML_2 component). You can also achieve this by clicking the Sync columns button in the Basic settings view of the tAdvancedFileOutputXML_2 component.
  6. In the Basic settings view of the tAdvancedFileOutputXML_2 component, click the Configure XML tree button to configure it as below. Follow the procedure used for the first subjob to set item as a loop element and set class as a group element.
  7. Select the Append the source xml file option.
  8. In the File Name field of the Basic settings view, enter the file path to the XML file specified in the first subjob ("D:/out.xml" in this example).
  9. Execute the Job and check if the file "D:/out.xml" generated corresponds to the xml file presented at the beginning of this document. As expected, the xml source file is appended with various loop elements.