xmllint如何读取具有android sharedpreferences值的xml文件

时间:2016-01-05 16:27:57

标签: xml shell xpath xmllint

我需要从android中的sharedPreference文件中获取值。该文件的xml如下所示:

<?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>

,文件名为fruits.xml

如何在shell脚本中使用xmllint来检索葡萄的值?

我已尝试xmllint fruits.xml然后xmllint xpath map/grapes,但它无效。我希望最终输出只是&#34; true&#34;因为这是葡萄的价值。

1 个答案:

答案 0 :(得分:1)

xmllint中此选项的正确名称为--xpath,路径表达式周围必须有引号("):

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

如果您使用的/map/boolean[@name = 'grapes']/@value没有字符串函数,xmllint将返回整个属性:value="true"

如果您不确定某个选项,只需输入

即可
$ xmllint

没有任何参数。这将带来帮助和列表,其中包括:

  

- xpath expr:评估XPath表达式,暗示--noout

我在Mac OS X上,使用以下版本的xmllint:

$ xmllint --version
xmllint: using libxml version 20902