Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
JeroenL
Contributor III
Contributor III

split xml file and save each file named as a value from the same xml file

Hello,

I have an XML file with a structure like below.

<?xml version="1.0" encoding="UTF-8"?>

<data>

 <loop>

   <ID>135358</ID>

   <MainData1>integer</MainData1>

   <MainData2>string</MainData2>

   <MainData3>string</MainData3>

   <sub_loop1>

     <looping_data>

       <Data1>integer</Data1>

       <Data2>string</Data2>

       <Data3>string</Data3>

       <Data4>float</Data4>

     </looping_data>

   </subloop1>

 </loop>

 <loop>

   <ID>135359</ID>

   <MainData1>integer</MainData1>

   <MainData2>string</MainData2>

   <MainData3>string</MainData3>

   <sub_loop1>

     <looping_data>

       <Data1>integer</Data1>

       <Data2>string</Data2>

       <Data3>string</Data3>

       <Data4>float</Data4>

     </looping_data>

   </subloop1>

 </loop>

 <loop>

   <ID>135360</ID>

   <MainData1>integer</MainData1>

   <MainData2>string</MainData2>

   <MainData3>string</MainData3>

   <sub_loop1>

     <looping_data>

       <Data1>integer</Data1>

       <Data2>string</Data2>

       <Data3>string</Data3>

       <Data4>float</Data4>

     </looping_data>

   </subloop1>

 </loop>

</data>

I would like this xml data to be saved as the following

file 1 = 135358.xml (with the data as shown below)

<data>

 <loop>

   <ID>135358</ID>

   <MainData1>integer</MainData1>

   <MainData2>string</MainData2>

   <MainData3>string</MainData3>

   <sub_loop1>

     <looping_data>

       <Data1>integer</Data1>

       <Data2>string</Data2>

       <Data3>string</Data3>

       <Data4>float</Data4>

     </looping_data>

   </subloop1>

 </loop>

</data>

file 2 = 135359.xml (with the data as shown below)

<data>

 <loop>

   <ID>135359</ID>

   <MainData1>integer</MainData1>

   <MainData2>string</MainData2>

   <MainData3>string</MainData3>

   <sub_loop1>

     <looping_data>

       <Data1>integer</Data1>

       <Data2>string</Data2>

       <Data3>string</Data3>

       <Data4>float</Data4>

     </looping_data>

   </subloop1>

 </loop>

</data>

file 3 = 135360.xml (with the data as shown below)

<data>

 <loop>

   <ID>135360</ID>

   <MainData1>integer</MainData1>

   <MainData2>string</MainData2>

   <MainData3>string</MainData3>

   <sub_loop1>

     <looping_data>

       <Data1>integer</Data1>

       <Data2>string</Data2>

       <Data3>string</Data3>

       <Data4>float</Data4>

     </looping_data>

   </subloop1>

 </loop>

</data>

If it's hard to name it using the <ID> field just a numeric count as filename is acceptable also.

Thanks in advance for your replies

Labels (3)
1 Solution

Accepted Solutions
gjeremy1617088143
Creator III
Creator III

0695b00000GhppeAAB.jpg0695b00000GhpXdAAJ.jpg0695b00000GhppAAAR.jpg0695b00000Ghpp5AAB.jpg0695b00000Ghpp0AAB.jpgHi @not specified not specified​ :

0695b00000GhpoqAAB.jpg0695b00000GhpogAAB.jpg0695b00000GhpoMAAR.jpgSend me Love and kudos

View solution in original post

9 Replies
gjeremy1617088143
Creator III
Creator III

Hi,

you can use a tExtractXMLFIeld with "data/loop" on Loop XPath query field , you set just one field on the component output type String :

and after you set the XPath query of the column to "."

you check GetNodes option

then you can add data tag like this "<data>" + your output string + "</data>" on a tjavarow or a tmap.

you can add this on a new field of the tjavarow or tmap :

(your input).replaceAll("(?s).*<ID>","").replaceAll("</ID>(?s).*","");

to get the value of ID

Send me Love and Kudos

JeroenL
Contributor III
Contributor III
Author

Hello and thx for your time looking into my question.

 

As far as i can follow your explanation, this is to write out the ID field using (your input).replaceAll("(?s).*<ID>","").replaceAll("</ID>(?s).*",""); and the rest is preparation for this.

 

I also have been trying to split the file using advancedfileoutputxml but the closest i have gotten is 1 first file including the first record and then a second file was created but empty.

 

Maybe it's easier if i can make a test project and share it here so it's easier for everyone to see exactly how it is/should be done?

Will do this later today/tomorrow and attach it to this thread.

gjeremy1617088143
Creator III
Creator III

Textractxmlfield with the options i set in the first message let you split the xml data as you want, then add data tag before and After the string to get the xml structure you want. The replace function permit you to get the id of each loop, then you can do a tflowtoiterate and iterate on tfileoutputraw with your id as filename.

JeroenL
Contributor III
Contributor III
Author

I'm getting a bit lost allrdy on the part to create 3 different xml from the basic xml file i have, it doesn't seem to add the data from the second loop the way i would like it to.

 

I added the project including the basic input and output file. hopefully u can guide me in the right way.

 

When u run this project u will see it doesn't add the data of the second loop.

 

Thanks allrdy for your time spend

gjeremy1617088143
Creator III
Creator III

0695b00000GhppeAAB.jpg0695b00000GhpXdAAJ.jpg0695b00000GhppAAAR.jpg0695b00000Ghpp5AAB.jpg0695b00000Ghpp0AAB.jpgHi @not specified not specified​ :

0695b00000GhpoqAAB.jpg0695b00000GhpogAAB.jpg0695b00000GhpoMAAR.jpgSend me Love and kudos

gjeremy1617088143
Creator III
Creator III

Hi @not specified not specified​ , did you Succeed in your XML split ?

Send Me Love and Kudos

JeroenL
Contributor III
Contributor III
Author

Hey, sorry for my late reply, changed the test to your setup but i don't seem to be able to put the same info in the trowgenerator see screenshot.

gjeremy1617088143
Creator III
Creator III

Hi, you have to set first the fields of the output schema of trowgenerator.

then you can fill them with the value you want.

JeroenL
Contributor III
Contributor III
Author

Worked as requested, thank you very much!