I have to parse xml to csv, using xmllint --shell or xmllint --xpath, because I'm not allowed to instal additional packages.
I need firstname and phone in the csv, and nothing else. I tried this to loop through an xml and parse it to csv file, but the problem is when First name has space (for example Mary Jane) or the phone is missing. Then this kind of solution does not work.
for f in $(echo 'cat //FIRSTNAME/text()' | xmllint --shell TEST.xml | sed '1d;$d' | sed 's/-------//')
do
echo $f >> $CSV_FILE_NAMES
done
for i in $(echo 'cat //HOMEPHONE/text()' | xmllint --shell TEST.xml | sed '1d;$d' | sed 's/-------//')
do
echo $i >> $CSV_FILE_PHONES
done
paste -d "," $CSV_FILE_NAMES $CSV_FILE_PHONES >> $CSV
Or this combined solution, which places every entity in a new line:
for f in $(echo 'cat //FIRSTNAME/text()|//HOMEPHONE/text()' | xmllint --shell TEST.xml | sed '1d;$d' | sed 's/-------//')
do
echo $f >> $CSV_FILE
done
Mark
9999999999
Jack
8888888888
Is there a different way to loop through an xml file?