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.
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.
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.
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 [email protected] or on the forum http://donkeydevelopment.com/forums.
Commercial use must be approved by donkeydevelopment.
Just send an email to [email protected] 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 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).
When you know the format of the
csv
file and the structure you need in thexml
file, it's fairly straightforward to make a script that can handle the conversion.Take the file
simple.csv
:You can create the following
xml
file:With the following script:
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. TheIFS=$','
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 theread
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 (namedarry
). 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 thexml
line is appended to the output file withecho
.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.
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:
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":
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.