converter xml para txt/csv: xml2 e XMLStarlet

dicas para converter xml para txt com xml2 e para csv com xmlstarlet

use o comando para instalar no ubuntu:
sudo apt-get install -y xml2 xmlstarlet

só ajeita o xml para texto para um formato de arvore, útil para ter uma visualização melhor
para mim, só esse trecho resolveu minha necessidade:
xml2 < agenda.xml > out.txt

Xml to csv


You didn’t supply sample input and desired output, so I couldn’t attempt a relevant demonstration.

Possible utilities:

XML2(1)                     General Commands Manual                    XML2(1)

       xml2 - convert xml documents in a flat format

       2xml - convert flat format into xml

       html2 - convert html documents in a flat format

       2html - convert flat format into html

       csv2 - convert csv files in a flat format

       2csv - convert flat format into csv





converte o xml para o formato csv, sendo necessário passar quais parâmetros vai querer na saída
para mim, só esse trecho resolveu minha necessidade:
xmlstarlet sel -T -t -m /ROOT/__Table -v “concat(COD,’;’,NOME,’;’,TELEFONE,’;’,CELULAR)” -n agenda.xml > out.csv

CSV using XMLStarlet

In this little tutorial I am going to describe a handy tool for transforming an XML document into a more easily processable CSV format. There are many ways of getting this job done – but most are more tedious than necessary (like writing a custom made RegEx parser – yuck!). Using XMLStarlet and XPath expressions this is going to be cinch. Let’s evaluate a number of typical XML data configurations and turn them into a flat CSV structure.

<key> value </key>


XMLStarlet can do a lot of stuff – we want to use it for querying / selecting from an XML document which we denote by selT or text  tells xmlstarlet to output text instead of XML. t m /root/record  or  template match /root/record specifies the section (or template) of the XML document which we would like to match repetitively – which is every <record> -section below  <root> . v  or  valueof  followed by an XPath expression specifies the string which we would like to output line by line – hence   n  or   nl  for newline. test.xml is … correct!

The XPath expressions are concatted and separated with semicolons. I guess there is not much more to add really as XPath is best understood by staring at it and in case you have to write a custom XPath query this site featuring a whole lot of examples for XPath expressions is pretty helpful.

<tag name=”key”> value </tag>


key[@name=‘C’] translates to “Get value of tag named ‘key’ if it features an attribute named ‘name’ valued ‘C’”.

<tag name=”key” val=”value” />


key[@name=‘D’]/@value) translates to “Get value of attribute named ‘value’ of tag named ‘key’ if that tag has an attribute ‘name’ with value ‘D’”.

<item> <key> K </key> <value> V </value> </item>


item[key=‘E’]/value translates to “Get value of tag ‘value’ below ‘item’ if this ‘item’ has an attribute named ‘key’ with value ‘E’.

V1 V2


Enjoy converting XML to CSV 🙂

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google

Você está comentando utilizando sua conta Google. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )

Conectando a %s