How can I convert a CSV file to XML?
Is there any software for Ubuntu?
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityOn the community website on converting there is a link to a command line tool called csv2xml. Since it is unmaintained you might want to choose another option.
There is also mention of a java tool called csv2xml (warning: website is in German) and a command line tool called ff-extractor.
The link also has references to Python, Perl, PHP, XSLT but that means you need to code the converter yourself.
When you know the format of the csv
file and the structure you need in the xml
file, it's fairly straightforward to make a script that can handle the conversion.
Take the file simple.csv
:
Jack,35,United States
Jill,22,United Kingdom
You can create the following xml
file:
<?xml version="1.0"?>
<Customers>
<Customer>
<Name>Jack</Name>
<Age>35</Age>
<Country>United States</Country>
</Customer>
<Customer>
<Name>Jill</Name>
<Age>22</Age>
<Country>United Kingdom</Country>
</Customer>
</Customers>
With the following script:
#!/bin/bash
file_in="simple.csv"
file_out="simple.xml"
echo '<?xml version="1.0"?>' > $file_out
echo '<Customers>' >> $file_out
while IFS=$',' read -r -a arry
do
echo ' <Customer>' >> $file_out
echo ' <Name>'${arry[0]}'</Name>' >> $file_out
echo ' <Age>'${arry[1]}'</Age>' >> $file_out
echo ' <Country>'${arry[2]}'</Country>' >> $file_out
echo ' </Customer>' >> $file_out
done < $file_in
echo '</Customers>' >> $file_out
Even if you have never coded before, I think this should be easy to use and modify. The file is read line-by-line in the while
loop.
IFS
is the internal field specifier. The IFS=$','
declares that the value of the field separator is a comma. This is standard for a CSV file, but it can be changed as needed to match the input file format.
The -r
argument to the read
command tells it to treat any backslashes in your file as part of your data rather than as an escape for a following special character.
The -a arry
argument places each column of your file into an array (named arry
). The columns in this example are name, age, country. In other words the values between the commas. So each column in the line is stored in an array.
Then the needed text for xml
is just wrapped around the values and the xml
line is appended to the output file with echo
.
while IFS=$',' read -r -a arry
, it is useful for another. Thanks.
– alhelal
Jan 25 '17 at 17:43
"Somename, Jack"
in place of Jack
in the input file?
– muk.li
Apr 8 '19 at 13:20
A fairly user-friendly (i.e., easy for numpties like me) solution to the CSV to XML conversion challenge is to use a nice cross-platform XML editor that has this feature built-in. (I've used it both under Ubuntu and Mac OSX 10.10.5; it also has Windows executable.)
As mentioned, it is an XML editor, but it includes CSV-to-XML (and Excel-to-XML) "import" on its main menu:
It converted a 31Mb CSV file for me (a dump from a library database of 20,000 entries) in about 15 seconds, giving me a well-formed XML file to save and manipulate.
As an editor it has many other welcome features (detailed at the link, above). I cannot find mention of any license for it, but this is included in the "README":
XMLSpear is free software for personal use.
Please send yor feedback to xmlspear@donkeydevelopment.com or on the forumhttp://donkeydevelopment.com/forums
.Commercial use must be approved by donkeydevelopment.
Just send an email to xmlspear@donkeydevelopment.com with the subject "licence request".
The readme also helpfully includes the contents for a .desktop file.
Does the job well for me under Ubuntu 18.04 LTS (Gnome).
I'm a big fan of BaseX
which seems to have an import capability:
http://docs.basex.org/wiki/CSV_Module
Might point out that it's, seemingly, easier to use a web application:
https://webapps.stackexchange.com/q/123959/24327
Which amounts to a plugin or extension to the browser which works with google sheets.
I feel your pain because it's a seemingly simple utility which should be, well, available through apt.
I would suggest you or someone write codes in Python. Python is easy to learn and solve your problem easily. It have both CSV module and XML modules. My suggestion takes into consideration that you might need to have your own names for XML elements or have other complex requirements (like converting last CSV column into attribute of last but one column).
There are tons of tutorial online about Python.