7

How can I convert a CSV file to XML?

Is there any software for Ubuntu?

  • I don't understand. The three format you mention are plain text files --- the difference is in content. And scope. Would you mind to add an example? – Rmano Feb 3 '15 at 15:50
  • 1
    Voting to re-open. I consider this pretty straightforward and easy to answer ;-) – Rinzwind Feb 3 '15 at 16:20
  • 1
    I support Rinzwind, because I just asked the software that can convert. – Vitor Mazuco Feb 3 '15 at 16:49
7

On 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.

  • 1
    I updated a little bit with the references in the 1st link. There is also a commercial product called Altova (altova.com). – Rinzwind Feb 3 '15 at 16:00
10

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.

  • @chaskes, if you will explain the option used in linewhile IFS=$',' read -r -a arry , it is useful for another. Thanks. – alhelal Jan 25 '17 at 17:43
  • @BandaMuhammadAlHelal Done. – chaskes Jan 25 '17 at 18:16
  • How would you deal with commas appearing within fields, surrounded by double quotes? As in "Somename, Jack" in place of Jack in the input file? – muk.li Apr 8 at 13:20
  • 1
    @muk.li The input field separator (usually a comma) is marked at the line starting: while IFS=$',' . Let's say the separator is an asterisk, you would just change it to: $'*' . So, in your case, it should be $'"' (single quote, double quote, single quote). This should work fine, but quoting in the shell can get tricky, and I haven't had a chance to test it. – chaskes Apr 9 at 17:26
1

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.)

XMLSpear

As mentioned, it is an XML editor, but it includes CSV-to-XML (and Excel-to-XML) "import" on its main menu:

enter image description here

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 forum http://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).

0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.