我想在查找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
并将状态设置为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?
最佳答案
您好像错过了此处的双引号:
String xpathexpression = "//task[@uuid=\"" + taskItems.get(position).get(task_uuid) + "\"]";
如果您改为使用单引号,则可能会更易读:
String xpathexpression = "//task[@uuid='" + taskItems.get(position).get(task_uuid) + "']";