简体   繁体   English

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

[英]How to edit attribute using XPath in Android

I want to use XPath to search for a task_status while looking for a uuid. 我想在寻找uuid的同时使用XPath搜索task_status

Here is my XML file: 这是我的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>

What I want is: search for uuid=AWFWF-C510-4E52-9026-020E3BFDDSG and set status = 1000. Both of them are attributes 我想要的是:搜索uuid=AWFWF-C510-4E52-9026-020E3BFDDSG并将状态设置为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);

Isn't that right? 是不是? I get the uuid from a ArrayList<HashMap<String,String>> . 我从ArrayList<HashMap<String,String>>获得uuid。 Maybe I do an mistake in the expression while adding the uuid from taskItems . 也许在从taskItems添加uuid时在表达式中犯了一个错误。

After I solved the Problem, how can I change the status to 1000? 解决问题后,如何将状态更改为1000?

Looks like you missed the closing double quotes here : 您好像错过了此处的双引号:

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

It may be slightly more readable if you use single quotes instead : 如果您改为使用单引号,则可能会更易读:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM