j2emanue j2emanue - 4 years ago 194
Bash Question

xmllint how to read xml file that has android sharedpreferences values

I need to get a value from a sharedPreference file in android. The xml for the file looks like this:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="apples" value="true" />
<boolean name="grapes" value="true" />
<boolean name="oranges" value="true" />
</map>


and the file name is fruits.xml

how can i use xmllint in a shell script to retrieve the value of grapes ?

I've tried
xmllint fruits.xml
and then
xmllint xpath map/grapes
but its not working. I want the final output to just be "true" as this is the value of grapes.

Answer Source

The correct name for this option in xmllint is --xpath and there must be quotes (") around the path expression:

$ xmllint --xpath "string(/map/boolean[@name = 'grapes']/@value)" fruit.xml
true

If you were using /map/boolean[@name = 'grapes']/@value without the string function around it, xmllint would return the whole attribute: value="true".

If you are unsure about an option simply type

$ xmllint

without any arguments. This will bring up the help and list, among other things:

--xpath expr: evaluate the XPath expression, imply --noout

I am on Mac OS X, using the following version of xmllint:

$ xmllint --version
xmllint: using libxml version 20902
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download