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

fonte: https://www.unix.com/unix-and-linux-applications/271281-xml-csv.html
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:
ex:
xml2 < agenda.xml > out.txt

Xml to csv

Hi.

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

Possible utilities:

Code:
XML2(1)                     General Commands Manual                    XML2(1)

NAME
       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

 

 

 


 

fonte: https://www.joyofdata.de/blog/transforming-xml-document-into-csv-using-xmlstarlet/
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:
ex:
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>

Solution:

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>

Solution:

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” />

Solution:

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>

Solution:

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

Solution:

Enjoy converting XML to CSV 🙂

Deixe um comentário

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

Logotipo do WordPress.com

Você está comentando utilizando sua conta WordPress.com. 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