Convert Call Logs Backup & Restore XML callist into csv

650 views Asked by At

I created one big file all.xml from all my call files with

echo '<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<!--File Created By Call Logs Backup & Restore v3.70 on 23/12/2016 03:02:21-->
<?xml-stylesheet type="text/xsl" href="calls.xsl"?>
<calls count="500">
'>all.xml
for i in calls-*.xml; do head -n-1 "$i"|tail -n+5; done>>all.xml
echo "</calls>">>all.xml

Now I try to export a callist from this format into csv:

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<!--File Created By Call Logs Backup & Restore v3.70 on 23/12/2016 03:02:21-->
<?xml-stylesheet type="text/xsl" href="calls.xsl"?>
<calls>
  <call number="+492345678" duration="0" date="1426694707547" type="3" readable_date="18.03.2015 17:05:07" contact_name="Someone" />
  <call number="+492345679" duration="3" date="1426695646292" type="2" readable_date="18.03.2015 17:20:46" contact_name="Someone else" />
  <call number="+492345670" duration="0" date="1426695687556" type="2" readable_date="18.03.2015 17:21:27" contact_name="Someone" />
</calls>

I tried with xmlstarlet:

xmlstarlet sel -B -t -m "//calls/call" -n -m "*" -v . -o , all.xml |less

but that just gives me an empty list, I guess, because there is no value in each call, but in the options of each call element.

I cannot find out with the help of the manuals, I found How do I get the options in a CSV?

2

There are 2 answers

1
Cyrus On BEST ANSWER

A different approach to convert individual xml attributes to csv:

xmlstarlet sel -t -m "//calls/call" -v "concat(@number,',',@duration,',',@date,',',@type,',',@readable_date,',',@contact_name)" -n file.xml

Output:

+492345678,0,1426694707547,3,18.03.2015 17:05:07,Someone
+492345679,3,1426695646292,2,18.03.2015 17:20:46,Someone else
+492345670,0,1426695687556,2,18.03.2015 17:21:27,Someone

1
Daniel Haley On

Try changing -m "*" to -m "@*"