One Star

How to generate XML with multi loop ?

Hi, 
I try to generate an xml with multiloop elements.
I have the following data recordset.

And I would like to generate this XML

<Entity code = "AL">				
<Store code = "10">
<Article code="AV2082">
<Locations>
<Location code="2111">
<Location code="2113">
<Location code="2115">
</Locations>
<BarCodes>
<cab code="222222">
<cab code="333333">
</BarCodes>
</Article>
<Article code="AV2085">
<Locations>
<Location code="2121">
</Locations>
<BarCodes>
<cab code="444444">
<cab code="555555">
</BarCodes>
</Article>
</Store>
</Entity >


I try with the tXMLMap or tWriteXMLField components but I don't manage to have all locations and baCodes for an article.
Is it possible to do this with talend ?
Thanks for your answer.
TOS 5.4.1 on Ubuntu 14.04.2 LTS
9 REPLIES
One Star

Re: How to generate XML with multi loop ?

I am running into same problem, where I am importing header from one file, details from another file, and payment info from another file.
My header remains the same, but item details have multiple item and payment could be from multiple credit card sources.
I have used tXMLMap for mapping and Advanced tAdvancedOutputXML, since I need output in XML file from flat file.
I did add schema for output, but it only allows loop once in one root tag. tWriteXMLField has the same issues.
Please help me.
Thanks for you time and response in advance.
Fifteen Stars

Re: How to generate XML with multi loop ?

This is tricky in Talend, but it is possible. There are two ways that I approach this with the Open Source Edition.
1) Create the XML in subsets. By that I mean, I analyse the structure and work out how many complex types there are with loops. Since the tXMLMap can only handle one loop, I create XML documents for each of the sub sections (maintaining a key column on the output to allow me to match it to the structure it will need to be added to. I then convert the XML to Strings and replace empty nodes with the computed loop section. It sounds complicated, but it is quite workable. As an example.....
Below is a simple example of XML with a nested loop. But in this piece of XML is treat the "innerLoop" as an entity with a value. The value is the "key" to the XML I will create later....
<myXML>
<myOuterLoop>
<myOuterLoopItem>
<someData>Blah blah blah</someData>
<innerLoop>OuterLoopItemKey1</innerLoop>
</myOuterLoopItem>
<myOuterLoopItem>
<someData>Yah Yah Yah</someData>
<innerLoop>OuterLoopItemKey2</innerLoop>
</myOuterLoopItem>
<myOuterLoopItem>
<someData>Lah Lah Lah</someData>
<innerLoop>OuterLoopItemKey3</innerLoop>
</myOuterLoopItem>
</myOuterLoop>
</myXML>

For the innerLoop XML, I create it using a tXMLMap and produce it alongside a "key" column. The "key" column contains the link between this document and the section of the above XML it needs to fit into. The "key" for the XML below will be "OuterLoopItemKey1".
<innerLoop>
<innerLoopItem>Something1</innerLoopItem>
<innerLoopItem>Something2</innerLoopItem>
<innerLoopItem>Something3</innerLoopItem>
<innerLoopItem>Something4</innerLoopItem>
</innerLoop>

Now I have produced the two pieces of XML, I convert them to Strings and use a find and replace mechanism to replace the "<innerLoop>OuterLoopItemKey1</innerLoop>" replace section of the main XML with the new computed loop corresponding to the key value "OuterLoopItemKey1".
This produces the output you will see below....
<myXML>
<myOuterLoop>
<myOuterLoopItem>
<someData>Blah blah blah</someData>
<innerLoop>
<innerLoopItem>Something1</innerLoopItem>
<innerLoopItem>Something2</innerLoopItem>
<innerLoopItem>Something3</innerLoopItem>
<innerLoopItem>Something4</innerLoopItem>
</innerLoop>
</myOuterLoopItem>
<myOuterLoopItem>
<someData>Yah Yah Yah</someData>
<innerLoop>OuterLoopItemKey2</innerLoop>
</myOuterLoopItem>
<myOuterLoopItem>
<someData>Lah Lah Lah</someData>
<innerLoop>OuterLoopItemKey3</innerLoop>
</myOuterLoopItem>
</myOuterLoop>
</myXML>

Yes, it is complicated. But it works and can be built in a structured methodical manner without having to know too much Java. I tend to use it for easier examples.
2) The other way I do it, I won't explain in detail. I will simply point to a reasonably straight forward example of using XStream (http://x-stream.github.io/tutorial.html). This requires Java, but is a really useful tool for complicated XML (there is also JAXB). For this you will need to create your required classes and use a tJavaFlex (maybe a tJavaRow) to pass your data to your classes. Once your classes are populated, you can convert them (serialise them) to an XML document.
Neither of these are ideal, I know. But they both work. 
Rilhia Solutions
One Star

Re: How to generate XML with multi loop ?

Thanks for your answer rhall_2.0
I chose your first solution.
I generated Sub XML for location for each article (= key) with a tXMLMap and I injected this sub XML in the main XML with another tXMLMap.
One Star

Re: How to generate XML with multi loop ?

Hi, 
I try to use tWriteXmlField with multiple loops but it's impossible.
My output is the content of xml generated  by tWriteXmlField  and not an xml file (that's why i didn't use tfileOutputMSXml).
Can you Help me plz?
One Star

Re: How to generate XML with multi loop ?

Hi, 
I try to use tWriteXmlField with multiple loops but it's impossible.
My output is the content of xml generated  by tWriteXmlField  and not an xml file (that's why i didn't use tXMLMap).
Can you Help me plz?

Sorry not able to help but tounsi? Smiley Tongue
One Star

Re: How to generate XML with multi loop ?

Hi, 
I try to use tWriteXmlField with multiple loops but it's impossible.
My output is the content of xml generated  by tWriteXmlField  and not an xml file (that's why i didn't use tfileOutputMSXml).
Can you Help me plz?

Sorry not able to help but tounsi? Smiley Tongue
yees
One Star

Re: How to generate XML with multi loop ?

Hi, 
I try to use tWriteXmlField with multiple loops but it's impossible.
My output is the content of xml generated  by tWriteXmlField  and not an xml file (that's why i didn't use tXMLMap).
Can you Help me plz?

Sorry not able to help but tounsi? Smiley Tongue
yees
We are everywhere Smiley Very Happy 
netcharfou Smiley Wink
One Star

Re: How to generate XML with multi loop ?

Hello,
I have the same problem if you can help me please
best regards
One Star

Re: How to generate XML with multi loop ?

Hello,
I have the same problem if you try neosize xl then help me please.
best regards

Did you try the solutions above amine?