Xpath 处理带有命名空间的xml


<?xml version="1.0" encoding="utf-8"?>
<process xmlns="http://jbpm.org/4.4/jpdl" key="payment" name="资金支付">
	<start name="start1" g="265,50,48,48">
		<transition name="to task1" to="填单" g="-53,-17"/>
	</start>
	<!-- candidate-users="1000201100000000036,1000201100000000034"  -->
	<task name="填单" g="243,132,92,52" assignee="1000201100000000036">
		<transition name="提交" to="exclusive1" g="-83,-17"/>
	</task>
	<decision name="exclusive1" g="266,208,48,48" expr="#{finance &gt; 500 ? '大量资金' : '一般资金'}" >
		<transition name="一般资金" to="一般资金" g="-53,-17"/>
		<transition name="大量资金" to="大量资金" g="-53,-17"/>

	</decision>
	<task name="一般资金" g="153,290,92,52" assignee="1000201100000000034">
		<transition name="通过" to="end1" g="-47,-17"/>
	</task>
	<task name="大量资金" g="332,288,92,52" assignee="1000201100000000033">
		<transition name="通过" to="end1" g="-47,-17"/>
	</task>
	<end name="end1" g="268,379,48,48"/>
</process>


SAXReader saxReader = new SAXReader();
	Document document;
	try {
		document = saxReader.read(new File(path + processKey));
		Map<String, String> nameSpaceMap = new HashMap<String, String>();
		nameSpaceMap.put("jbpm", "http://jbpm.org/4.4/jpdl");
		//设置命名空间
		saxReader.getDocumentFactory().setXPathNamespaceURIs(nameSpaceMap);
		//使用xpath找节点的时候前面加  jbpm:  就可以找到。
		Element eleTask = (Element) document.selectSingleNode("/process/jbpm:decision[@name='" + nodeName + "']");
		OutputFormat format = new OutputFormat("", false, "utf-8"); // 设置输出格式为空两格,换行,编码为utf-8
		XMLWriter writer = new XMLWriter(new FileOutputStream(path +processKey), format); // 创建输出对象
		writer.write(document);
		writer.close();
	} catch (DocumentException e1) {
		e1.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}

你可能感兴趣的:(xml,jbpm)