未在XML节点转换中声明命名空间

时间:2016-09-07 13:01:50

标签: java xml dom bpmn

我正在尝试将bpmn文件处理为自己的流模型。实际上,我的问题与bpmn标准完全无关,因此将其视为上下文问题。 我想获取一个xml节点并将其转换为String,以便以后保存到数据库。 我在下面的代码中尝试做的是使用xpath获取BPMNDiagram节点,并将其导出为字符串但是,当我尝试导出时,我得到一个关于不声明nsi命名空间的说明。 我在xpath之前声明了所有命名空间" query"但是一旦我得到这个节点并尝试转换它,我得到下面描述的错误。 由于我正在获得正确的节点,因此xpath部分正常工作。问题出现在转型阶段。

XML文件(部分)

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL"xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"     xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.2.1">
<bpmn:process id="PP-ProcessProva01" name="ProcesProva" isExecutable="true">
...
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="PP-ProcessProva01">
  <bpmndi:BPMNShape id="StartEvent_1cp968c_di" bpmnElement="PP_EV_ENTRADA">
    <dc:Bounds x="-39" y="143" width="36" height="36" />
    <bpmndi:BPMNLabel>
      <dc:Bounds x="70" y="161" width="90" height="20" />
    </bpmndi:BPMNLabel>
  </bpmndi:BPMNShape>
  <bpmndi:BPMNShape id="Task_0ogrwwq_di" bpmnElement="PP_AC_VALIDACION">
    <dc:Bounds x="241.17552742616033" y="120.96118143459915" width="100" height="80" />
  </bpmndi:BPMNShape>
  <bpmndi:BPMNEdge id="SequenceFlow_1bc244v_di" bpmnElement="EV_TR_PP_EV_ENTRADA-PP_AC_VALIDACION">
    <di:waypoint xsi:type="dc:Point" x="-3" y="161" />
    <di:waypoint xsi:type="dc:Point" x="241" y="161" />
    <bpmndi:BPMNLabel>
      <dc:Bounds x="21.459854014598534" y="151" width="90" height="20" />
    </bpmndi:BPMNLabel>
  </bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>

这是我的代码:

String res="";
File file2 = new File("c:\\temp\\prova.bpmn");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
org.w3c.dom.Document doc = dbf.newDocumentBuilder().parse(file2);

HashMap<String, String> prefMap = new HashMap<String, String>() {{
                put("bpmn", "http://www.omg.org/spec/BPMN/20100524/MODEL");
                put("bpmndi", "http://www.omg.org/spec/BPMN/20100524/DI");
                put("di", "http://www.omg.org/spec/DD/20100524/DI");
                put("dc", "http://www.omg.org/spec/DD/20100524/DC");
                put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                put("camunda", "http://camunda.org/schema/1.0/bpmn");
            }};
SimpleNamespaceContext namespaces = new SimpleNamespaceContext(prefMap);

javax.xml.xpath.XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(namespaces);
javax.xml.xpath.XPathExpression expr = xpath.compile("/definitions/BPMNDiagram");
Node nodeDi = (Node) expr.evaluate(doc,XPathConstants.NODE);



Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(nodeDi), new StreamResult(res));

错误讯息:

Namespace for prefix 'nsi' has not been declared

我是否必须以类似的方式声明转换级别的命名空间?请有人帮助我吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

根据Martin Honnen的评论,我可以解决我的问题:

&#34; 请注意,XSLT和XPath需要一个名称空间感知的DocumentBuilderFactory,因此在创建DocumentBuilders并使用命名空间解析XML文档之前,请确保首先在DocumentBuilderFactory上使用setNamespaceAware(true)&#34;

相关问题