Six Stars

How to merge xml nodes from tXMLMap output

Hi, all!

I have tXMLMap, which prodused xml, like below:

<Employee>
 <Name> 1st </Name>
 <Age> 24 </Age>
 <Roles>
<Role>
<RoleName> One </RoleName>
</Role>
</Roles> </Employee>
<Employee>
<Name> 1st </Name>
<Age> 24 </Age>
<Roles>

<Role>
<RoleName> Two </RoleName>
</Role>
</Roles>
</Employee>

How to process this output to reach result, like below:

<Employee> 
  <Name> 1st </Name> 
  <Age> 24 </Age>
  <Roles>
    <Role>
        <RoleName> One </RoleName>
    </Role>
    <Role>
        <RoleName> Two </RoleName>
    </Role>
  </Roles> 
</Employee> 

Regards

 

  • ESB
Tags (1)
5 REPLIES
Ten Stars

Re: How to merge xml nodes from tXMLMap output

Six Stars

Re: How to merge xml nodes from tXMLMap output

Hi, rhall_2_0!

Thank you for reply, but i have embedded loops: Employee is parent for Role. When i try to set second loop, it erased  Smiley Sad

Ten Stars

Re: How to merge xml nodes from tXMLMap output

Ah I see. This is a bit of a pain, but can still be done. It is just a bit more complicated. This is a high level of the an approach that I use for such occasions (when I am not using Talend Data Mapper). Build your XML in two (or more) steps.

 

1) Build your Employee loops as complete XML files disassociated from your main body. This way you can handle the Role loops. Then convert them to Strings and remove the XML declaration from the Strings.

2) Build the outer body of your XML with an Element that will be replaced by your Employee loop. Convert that to a String.

3)  Use String utils to replace the Element in your outer XML with your concatenated looping Employee sections.

 

For example....

Outer XML

<outer_xml>
<data1></data1>
<data2></data2>
<my_replace_element/>
</outer_xml>

Employee XML (first)

<Employee> 
  <Name> 1st </Name> 
  <Age> 24 </Age>
  <Roles>
    <Role>
        <RoleName> One </RoleName>
    </Role>
    <Role>
        <RoleName> Two </RoleName>
    </Role>
  </Roles> 
</Employee> 

Employee XML (second)

<Employee> 
  <Name> 2nd </Name> 
  <Age> 27 </Age>
  <Roles>
    <Role>
        <RoleName> Three </RoleName>
    </Role>
    <Role>
        <RoleName> Four </RoleName>
    </Role>
  </Roles> 
</Employee>

 Combined Employee XML (using STring concatenation)

<Employee> 
  <Name> 1st </Name> 
  <Age> 24 </Age>
  <Roles>
    <Role>
        <RoleName> One </RoleName>
    </Role>
    <Role>
        <RoleName> Two </RoleName>
    </Role>
  </Roles> 
</Employee> 
<Employee>
<Name> 2nd </Name>
<Age> 27 </Age>
<Roles>
<Role>
<RoleName> Three </RoleName>
</Role>
<Role>
<RoleName> Four </RoleName>
</Role>
</Roles>
</Employee>

Employee XML Loop Merged with Outer XML

<outer_xml>
<data1></data1>
<data2></data2>

<Employee>
<Name> 1st </Name>
<Age> 24 </Age>
<Roles>
<Role>
<RoleName> One </RoleName>
</Role>
<Role>
<RoleName> Two </RoleName>
</Role>
</Roles>
</Employee>
<Employee>
<Name> 2nd </Name>
<Age> 27 </Age>
<Roles>
<Role>
<RoleName> Three </RoleName>
</Role>
<Role>
<RoleName> Four </RoleName>
</Role>
</Roles>
</Employee>

</outer_xml>


The concatenated Employee XMLs replace the "<my_replace_element/>" using Java String utils.

 

Once you have your XML String sorted, you can use a tConvertType to convert from a String XML representation to a Document.

It looks complicated but is actually pretty straight forward once you get your head around it.

 

Rilhia Solutions
Six Stars

Re: How to merge xml nodes from tXMLMap output

Sorry, I'm a little confused. In all the examples, you showed the target result:

<Employee> (loop)
     <data></data>
      <Roles>
           <Role> (loop)
           </Role>
       </Roles>
</Employee>

How do I act?

 

 

 

 

Ten Stars

Re: How to merge xml nodes from tXMLMap output

Think about 2 structures; your Employee structure which you can build with the role loop internal if it is considered a single standalone XML, and your outer structure.

 

You will use at least 2 tXMLMap components for this. One to build your Employee XMLs and one to build your outer structure.

 

Since you are building your Employee XML as separate XMLs, your Role loop will work inside this. Once each Employee XML is built, you concatenate them and use String utils to add them to your Outer XML (using the replace mechanism I described). 

There was no need for me to mention the structure you are currently stuck with since this method will mean you never get that structure.

Rilhia Solutions