java xml 操作 getElementById 获取到的总是 null
巴扎黑
巴扎黑 2017-04-18 10:33:47
0
1
709

用的是 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。但是还是不会修改。

巴扎黑
巴扎黑

全員に返信(1)
大家讲道理

XML ファイルと Java コードの内容を投稿してください。ツールの問題を疑う必要はありません。繰り返し使用してテストおよび検証されており、簡単に問題が発生することはありません。


【補足内容】

Document.getElementById()メソッドのjavadoc記述によれば、基本的にID型の名前は事前定義されていない、つまり「ID」や「id」ではないと判断されます。 以下はjavadoc記述です。 :

指定された値の ID 属性を持つ要素を返します。そのような要素が存在しない場合は、 null を返します。その値の ID 属性を持つ要素が複数ある場合、返される内容は未定義です。

DOM 実装が期待されます。属性 Attr.isId を使用して、属性が ID 型であるかどうかを判断します。

注: Attributes with the name "ID" or "id" are not of type ID unless so defined.
パラメータ:elementId 要素の一意の ID 値。

戻り値:一致する要素、または null の場合何もありません。

以降:DOM レベル 2

解決策は 3 つあります。

1. Attr インターフェイスを継承して、ID 型名のバインディングを完了するためにカスタム クラスを書き換えます。
2. XPath ツールを使用します。 (推奨)

注: stackoverflow に関連する質問があります。リンクを参照してください: http://stackoverflow.com/ques...

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート