I have xml file with multiple <db>
elements, I need to extract attributes from each node and do some work on them.
IS it possible to load them to array, using ksh and xmllint ?
This is what I have:
xml file:
<?xml version="1.0"?>
<main>
<resources>
<db>
<table_name tableid="001" user="mike">customer</table_name>
</db>
<db>
<table_name tableid="002" user="david">orders</table_name>
</db>
</resources>
</main>
script:
#!/usr/bin/ksh
tbid="$(echo "cat /main/resources/db/table_name/@tableid" | xmllint --shell data.xml )"
username="$(echo "cat /main/resources/db/table_name/@user" | xmllint --shell data.xml )"
echo $tbid
echo $username
Output:
/ > ------- tableid="001" ------- tableid="002" / >
/ > ------- user="mike" ------- user="david" / >
eventually, I want to get a kind of 2-dim array:
arr[0],[0]=001
arr[0],[1]=mike
arr[1],[0]=002
arr[1],[1]=david
Notes:
xpath is not supported in xmllint , and can't be installed.
array can be represented in any other way, I just need to be able to extract and do some work.
perl
?, becauseXML::Twig
is really good for this sort of job. – Sobrique Dec 18 '15 at 17:14