Ubuntu – From XML to CSV with command line

command linecsvxlsxml

if I have a XML file on local disk, which command is need to convert file in CSV?

And from XLSX or XLS to CVS?

Thanks.

Best Answer

You can use xml2 utility (https://manpages.debian.org/jessie/xml2/2xml.1) and starting in example from

<ANCFModel>

  <UNITS force="NEWTON" mass="KILOGRAM" length="MILLIMETER" time="SECOND"/>
  <GRID id="301019" x="328.217224" y="543.844177" z="0.000000" rx="0.000000 -1.000000 0.000000" ry="1.000000 0.000000 0.000000" rz="0.000000 0.000000 1.000000"/>
  <GRID id="303001" x="328.217224" y="518.844177" z="0.000000" rx="0.000000 -1.000000 0.000000" ry="1.000000 0.000000 0.000000" rz="0.000000 0.000000 1.000000"/>
  <GRID id="303002" x="328.217224" y="493.844177" z="0.000000" rx="0.000000 -1.000000 0.000000" ry="1.000000 0.000000 0.000000" rz="0.000000 0.000000 1.000000"/>
  <GRID id="303003" x="328.217224" y="468.844177" z="0.000000" rx="0.000000 -1.000000 0.000000" ry="1.000000 0.000000 0.000000" rz="0.000000 0.000000 1.000000"/>
  <GRID id="301020" x="328.217224" y="443.844177" z="0.000000" rx="0.000000 -1.000000 0.000000" ry="1.000000 0.000000 0.000000" rz="0.000000 0.000000 1.000000"/>
  <BEAM12 id="20000000" pid="10000" g1="301019" g2="303001"/>
  <BEAM12 id="20000001" pid="10000" g1="303001" g2="303002"/>
  <BEAM12 id="20000002" pid="10000" g1="303002" g2="303003"/>
  <BEAM12 id="20000003" pid="10000" g1="303003" g2="301020"/>
  <PBEAML id="10000" mid="3000000" type="BAR" dim1a="5.000000" dim2a="50.000000" dim1b="5.000000" dim2b="50.000000" nx="5" ny="3" nz="3" ngx="5" ngy="4" ngz="4"/>
  <MAT1 id="3000000" e="210000" nu="0.3" rho="7.86e-06" YS="0.001" AP="2"/>
  <CONN0 id="40000" gid="301020" conn="TTTTTT"/>
  <CONN0 id="40001" gid="301019" conn="TTTTTT"/>
</ANCFModel>

and running

xml2 <  input.xml | 2csv GRID @id @x @y @z @rx @ry @rz

you will have

+--------+------------+------------+----------+-----------------------------+----------------------------+----------------------------+
| 301019 | 328.217224 | 543.844177 | 0.000000 | 0.000000 -1.000000 0.000000 | 1.000000 0.000000 0.000000 | 0.000000 0.000000 1.000000 |
| 303001 | 328.217224 | 518.844177 | 0.000000 | 0.000000 -1.000000 0.000000 | 1.000000 0.000000 0.000000 | 0.000000 0.000000 1.000000 |
| 303002 | 328.217224 | 493.844177 | 0.000000 | 0.000000 -1.000000 0.000000 | 1.000000 0.000000 0.000000 | 0.000000 0.000000 1.000000 |
| 303003 | 328.217224 | 468.844177 | 0.000000 | 0.000000 -1.000000 0.000000 | 1.000000 0.000000 0.000000 | 0.000000 0.000000 1.000000 |
| 301020 | 328.217224 | 443.844177 | 0.000000 | 0.000000 -1.000000 0.000000 | 1.000000 0.000000 0.000000 | 0.000000 0.000000 1.000000 |
+--------+------------+------------+----------+-----------------------------+----------------------------+----------------------------+