如何在Android中使用XPath编辑属性

时间:2015-08-18 07:20:58

标签: java android xml xpath

我想在寻找uuid的同时使用XPath搜索task_status

这是我的XML文件:

    <?xml version="1.0" encoding="utf-8"?>
<Test1>
<typ>task</typ>
  <datestamp>20150602153306</datestamp>
  <datecreate>20150602153306</datecreate>
<task uuid="92F7F685-C370-4E55-9026-020E3CDCEDE0" status="0">
    <task_headline>TEST2000</task_headline>
    <task_subject>There is a Problem.</task_subject>
    <task_action>Solve it!</task_action>
    <task_priority color="#E62C29">high</task_priority>
    <task_status>200</task_status>
    <task_note></task_note>
</task>
<task uuid="AWFWF-C510-4E52-9026-020E3BFDDSG" status="0">
    <task_headline>TEST3000</task_headline>
    <task_subject>Another Problem</task_subject>
    <task_action>Solve it again.</task_action>
    <task_priority color="#E62C29">high</task_priority>
    <task_status>200</task_status>
</task>
</Test1>

我想要的是:搜索uuid=AWFWF-C510-4E52-9026-020E3BFDDSG并设置status = 1000。 它们都是属性

    XPathFactory xPathFactory = XPathFactory.newInstance();
                        XPath xPath = xPathFactory.newXPath();
                        String xpathexpression = "//task[@uuid=\"" + taskItems.get(position).get(task_uuid) + "]";
XPathExpression expression = xPath.compile(xpathexpression);
                        NodeList nl = (NodeList) expression.evaluate(doc, XPathConstants.NODESET);

不是吗? 我从ArrayList<HashMap<String,String>>获得了uuid。 也许我在添加来自taskItems的uuid时在表达式中犯了一个错误。

解决问题后,如何将状态更改为1000?

1 个答案:

答案 0 :(得分:1)

看起来你错过了这里的收尾双引号:

String xpathexpression = "//task[@uuid=\"" + taskItems.get(position).get(task_uuid) + "\"]";

如果使用单引号,它可能会稍微更具可读性:

String xpathexpression = "//task[@uuid='" + taskItems.get(position).get(task_uuid) + "']";