Support Questions

Find answers, ask questions, and share your expertise
Announcements
Welcome to the upgraded Community! Read this blog to see What’s New!

How to Convert XML file into CSV file using NIFI

avatar

i have XML files about 1-2 GB i want it to convert into CSV format using apache nifi.

so that i can process CSV for joining the two csv.

1 ACCEPTED SOLUTION

avatar
Guru

Hi @Nitin Damle

You can use the ConvertRecord processor (with XMLReader/CSVRecordSetWriter controller services). I have an article here that shows CSV to XML (with CSVReader/XMLRecordSetWriter controller services), so you just need to do the inverse of that record transformation:

https://community.hortonworks.com/content/kbentry/199310/xml-record-writer-in-apache-nifi-170.html

-Andrew

View solution in original post

22 REPLIES 22

avatar
Guru

Hi @Nitin Damle

You can use the ConvertRecord processor (with XMLReader/CSVRecordSetWriter controller services). I have an article here that shows CSV to XML (with CSVReader/XMLRecordSetWriter controller services), so you just need to do the inverse of that record transformation:

https://community.hortonworks.com/content/kbentry/199310/xml-record-writer-in-apache-nifi-170.html

-Andrew

avatar

@Andrew Lim

can you please upload the template here i have tried to upload template given in link but it throwing error

avatar
Guru

@Nitin Damle

convert-cvs-to-xml.xml

If you are still having problems uploading the template, can you confirm what version of NiFi you are on? I just did a test in NiFi 1.8.0 and the upload was successful.

avatar
Guru

Hi @Nitin Damle

It would be helpful if you selected "Reply" to each of my comments instead of adding new Answers, which makes the readability of this question more difficult. Understandably, the "Reply" link is very small and hard to see.

Are you modifying the template prior to uploading it into NiFi? That was the issue in the other HCC article you referenced (https://community.hortonworks.com/questions/226390/nifi-18-error-updateattribute-is-not-known-to-thi...). Because the template was malformed, it gave that error.

avatar

@Andrew Lim can you upload the template for XML to CSV with sample data .


avatar
Guru

Hi @Nitin Damle

Here is the XML to CSV template: convert-xml-to-csv.xml

Here is the sample XML file to use: users.xml

If you find these helpful, please "Accept" my original answer from February 14th as this template is the implementation of what I suggested to do.

avatar
Guru

One thing, I wanted to note. For the XMLReader controller service (https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-record-serialization-services...), I needed to set the Expect Records as Array property to "true".

avatar

Thanks @Andrew Lim

avatar

@Andrew Lim , i am getting header only not data


cm_samples.xml


schema.txt


i attached my schema and xml pls let me know where i am going wrong

avatar
Guru

Hi @Nitin Damle

I'm assuming you got the flow working with the sample data and template I provided.

Can you provide an example of the desired csv output you are hoping to achieve from the cm_samples.xml file you provided? That would help me understand your end goal for the flow.

Thanks!

avatar

xmlsampleop.png


@Andrew Lim you can refercm_sample.txt


this image and file


avatar
Guru

Thanks for the information Nitin.

It seems like it would make sense to "clean up" your incoming XML using the TransformXML processor (https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.6.0/org.apache...) prior to ConvertRecord since there is data there like the header info that you don't care about for the CSV file. Additionally, could change the tag from `p` to its `name` attribute.

I haven't used the TransformXML processor before (or worked with XSLT files before). Take a look yourself and see if you think it could be a solution for you. I will try it out myself if I get the opportunity.

avatar

Hi Andrew,

I am not able to convert xml to csv as suggested by you to do the inverse of this. Could you please provide the nifi xml file so that I can upload the template and check.


avatar
Guru

avatar

Hi Andrew , this works for single xml but I have nested xml. How to convert nested xml to multiple csvs ?

avatar

@Andrew Lim Is there anything possible for nested xml to multiple csvs ?Attached is the nested xml file ad.xml

avatar

@Andrew Lim After importing it giving me this error

106511-csvtoxmlerror.png

avatar

You can use this NIFI Groovy processor that converts XML to CSV or AVRO

https://github.com/maxbback/nifi-xml


avatar

@Max Börebäck can you upload the template here

Labels