用的是 org.w3c.dom 中的类
我确定 id 存在的,但是获取到的总是 null。
测试代码如下:
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class XmlTest {
private static final String originXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<bpmn2:definitions xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:bpmn2=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:dc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:di=\"http://www.omg.org/spec/DD/20100524/DI\" xmlns:camunda=\"http://camunda.org/schema/1.0/bpmn\" ID=\"sample-diagram\" targetNamespace=\"http://bpmn.io/schema/bpmn\" xsi:schemaLocation=\"http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd\">\n" +
" <bpmn2:process ID=\"Process_1\" isExecutable=\"false\">\n" +
" <bpmn2:startEvent ID=\"StartEvent_1w6y3xa\">\n" +
" <bpmn2:documentation>com.sunyard.dragon.organ.Terminal.start#.</bpmn2:documentation>\n" +
" <bpmn2:outgoing>SequenceFlow_0dx3fgt</bpmn2:outgoing>\n" +
" </bpmn2:startEvent>\n" +
" <bpmn2:task ID=\"Task_0mhraek\">\n" +
" <bpmn2:documentation>com.sunyard.dragon.organ.Switch.branch#S.</bpmn2:documentation>\n" +
" <bpmn2:extensionElements>\n" +
" <camunda:inputOutput>\n" +
" <camunda:inputParameter name=\"Input_2176611\">\n" +
" <camunda:script scriptFormat=\"S\">xml./we/request/params</camunda:script>\n" +
" </camunda:inputParameter>\n" +
" </camunda:inputOutput>\n" +
" </bpmn2:extensionElements>\n" +
" <bpmn2:incoming>SequenceFlow_0dx3fgt</bpmn2:incoming>\n" +
" </bpmn2:task>\n" +
" <bpmn2:sequenceFlow ID=\"SequenceFlow_0dx3fgt\" sourceRef=\"StartEvent_1w6y3xa\" targetRef=\"Task_0mhraek\" />\n" +
" </bpmn2:process>\n" +
" <bpmndi:BPMNDiagram ID=\"BPMNDiagram_1\">\n" +
" <bpmndi:BPMNPlane ID=\"BPMNPlane_1\" bpmnElement=\"Process_1\">\n" +
" <bpmndi:BPMNShape ID=\"StartEvent_1w6y3xa_di\" bpmnElement=\"StartEvent_1w6y3xa\">\n" +
" <dc:Bounds x=\"484\" y=\"92\" width=\"36\" height=\"36\" />\n" +
" <bpmndi:BPMNLabel>\n" +
" <dc:Bounds x=\"502\" y=\"128\" width=\"0\" height=\"0\" />\n" +
" </bpmndi:BPMNLabel>\n" +
" </bpmndi:BPMNShape>\n" +
" <bpmndi:BPMNShape ID=\"Task_0mhraek_di\" bpmnElement=\"Task_0mhraek\">\n" +
" <dc:Bounds x=\"452\" y=\"184\" width=\"100\" height=\"80\" />\n" +
" </bpmndi:BPMNShape>\n" +
" <bpmndi:BPMNEdge ID=\"SequenceFlow_0dx3fgt_di\" bpmnElement=\"SequenceFlow_0dx3fgt\">\n" +
" <di:waypoint xsi:type=\"dc:Point\" x=\"502\" y=\"128\" />\n" +
" <di:waypoint xsi:type=\"dc:Point\" x=\"502\" y=\"184\" />\n" +
" <bpmndi:BPMNLabel>\n" +
" <dc:Bounds x=\"517\" y=\"146\" width=\"0\" height=\"0\" />\n" +
" </bpmndi:BPMNLabel>\n" +
" </bpmndi:BPMNEdge>\n" +
" </bpmndi:BPMNPlane>\n" +
" </bpmndi:BPMNDiagram>\n" +
"</bpmn2:definitions>\n";
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream(originXml.getBytes("UTF-8"));
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("bpmn2:task");
for (int i = 0; i < list.getLength() ; i++){
System.out.println(list.item(i));
}
System.out.println(doc.getElementById("Task_0mhraek"));
}
}
经过搜索认为问题是没有在 DTD 中定义 ID 是 ID。但是还是不会修改。
XML 파일과 Java 코드의 내용을 게시하고 도구의 문제를 의심하지 마십시오. 반복적인 사용을 통해 테스트 및 검증되었으므로 쉽게 문제가 발생하지 않습니다
【보충내용】
Document.getElementById() 메소드의 javadoc 설명에 따르면 기본적으로 ID 유형의 이름이 미리 정의되어 있지 않은 즉, "ID" 또는 "id"가 아닌 것으로 판단됩니다. javadoc 설명:
세 가지 해결 방법이 있습니다.
1. Attr 인터페이스를 상속하고 사용자 정의 클래스를 다시 작성하여 ID 유형 이름 바인딩을 완료합니다.
2. Document.getElementById() 메서드를 다시 작성합니다.
3. 도구. (권장)
참고: stackoverflow에 관련된 질문이 있습니다. http://stackoverflow.com/ques...
링크를 참조하세요.