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

xmllint how to read xml file that has android sharedpreferences values

我需要从 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 但它不起作用。我希望最终输出只是 "true",因为这是葡萄的价值。

此选项在 xmllint 中的正确名称是 --xpath,路径表达式两边必须有引号 ("):

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

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

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

$ xmllint

没有任何争论。这将调出帮助和列表,以及其他内容:

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

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

$ xmllint --version
xmllint: using libxml version 20902