Xml file parsing in Linux

Xml file parsing in Linux via command line, I once thought it is not possible better create simple Java application. I have heard about “xmllint – command line XML tool” once, hence thought of giving a try,

My xml content is something like below,

<table>
<name>STUDENT</name>
<canUpdate>true</canUpdate>
<canDelete>false</canDelete>
<canRun>false</canRun>
<columnDetails>
......
</columnDetails>
.....
</table>
<table>
<canUpdate>true</canUpdate>
<canDelete>false</canDelete>
<canRun>false</canRun>
....
...

My requirement here is to get the list of table name using single command in linux. I ended up in using following command. I know there could be a better version and, if you know, provide it in comment section,

xmllint --xpath '//tableInfo/name' content.xml  | sed "s|</name>|,|g" | sed "s|<name>| |g"

If you want to get the list name with out formatting,

xmllint --xpath '//tableInfo/name content.xml
xmllint --xpath '//tableInfo/name/text()' content.xml 

Leave a Reply

Your email address will not be published.

WP2Social Auto Publish Powered By : XYZScripts.com