Skip to main content
Announcements
SYSTEM MAINTENANCE: Thurs., Sept. 19, 1 AM ET, Platform will be unavailable for approx. 60 minutes.
cancel
Showing results for 
Search instead for 
Did you mean: 
solquest
Contributor
Contributor

from different XML nodes to array

Hey there.

I'm almost a newbie with Talend...I have an XML file and I have to put its different nodes in the same output node.

This is the input

<product>

   <code>XXX</code>

       <telephone>

           <code>telephone</code>

           <elements>

               <element>

                   <item>

                       <value>

                           <type>TEXT</type>

                           <code>value</code>

                           <value>999 999 999</value>

                       </value>

                   </item>

               </element>

           </elements>

       </telephone>

       <email>

           <code>email</code>

           <elements>

               <element>

                   <item>

                       <value>

                           <type>TEXT</type>

                           <code>value</code>

                           <value>info@email.com</value>

                       </value>

                   </item>

               </element>

           </elements>

       </email>

       <website>

           <code>website</code>

           <elements>

               <element>

                   <item>

                       <value>

                           <type>TEXT</type>

                           <code>value</code>

                           <value>www.website.com</value>

                       </value>

                   </item>

               </element>

           </elements>

       </website>

</product>

where those <telephone>, <email>, <website> nodes are optional.

while the output should be

<product>

   <code>XXX</code>

       <contact>

           <code>contact</code>

           <elements>

               <element>

                   <item>

                       <value>

                           <type>TEXT</type>

                           <name>Telephone</code>

                           <value>999 999 999</value>

                       </value>

                       <value>

                           <type>TEXT</type>

                           <name>Email</code>

                           <value>info@email.com</value>

                       </value>

                       <value>

                           <type>TEXT</type>

                           <name>Website</code>

                           <value>www.website.com</value>

                       </value>

                   </item>

               </element>

           </elements>

       </contact>

</product>

I have no idea I can achieve this using tXMLMap.

Can anyone point me in the right direction?

Thanks.

Labels (2)
2 Replies
David_Beaty
Specialist
Specialist

Hi,

 

You're going to need to construct the whole XML structure as if all the data was coming in (as per your spec above), but ensure that you also enable the option to not generate null/blank entries. You then have to also get the tXMLMap to create the values for <type> and <name> if the <value> is present or blank. IE is the email VALUE is null, then also make the email TYPE null and the email NAME null. When it constructs, it then won't build the whole <value> section at all.

 

Thanks

 

David

solquest
Contributor
Contributor
Author

Thanks.

It looks similiar to what I ended up with, when I realized I had no chance to do it in the same tXMLMap where I was mapping all other fields.

So now at first I'm taking only that part of original xml file, creating the output xml structure and store it in a context variable.

After that, in the full tXMLMap I'm writing that context variable in the final xml.

Thanks!