首页 > 解决方案 > 如何通过 JDOM 解析 xml 文件以找到主要属性的内容

问题描述

我想解析我的 xml 文件(BPMN 2.0)以<text>通过 JDOM 读取标签的内容。我的意思是阅读“我的注释测试”

  <textAnnotation id="myAnnotation_ID" signavio:alignment="left" textFormat="text/plain">
     <text>Test of myAnnotation</text>
  </textAnnotation>

这是我的代码:

Document doc = new SAXBuilder().build(myfile);
BPMN2NS = Namespace.getNamespace("http://www.omg.org/spec/BPMN/20100524/MODEL");
Element procElem = doc.getRootElement().getChild("process", BPMN2NS);
List<Element> textAnnotation = procElem.getChildren("textAnnotation", BPMN2NS);

但是我已经可以阅读的是[Element: <text [Namespace: http://www.omg.org/spec/BPMN/20100524/MODEL]/>],“textAnnotation”元素的“内容”。

知道如何阅读“MyAnnotation 测试”吗?

标签: javaxmlbpmnjdom

解决方案


我想一旦你得到你的 textAnnotation 元素,你只需要得到所有名为“text”的孩子,并使用下面的代码来获取里面的文本。

    Document doc = new SAXBuilder().build(myfile);

    Namespace BPMN2NS = Namespace.getNamespace("http://www.omg.org/spec/BPMN/20100524/MODEL");
    Element procElem = doc.getRootElement().getChild("process", BPMN2NS);
    List<Element> textAnnotations = procElem.getChildren("textAnnotation", BPMN2NS);
    List<Element> texts = textAnnotations.get(0).getChildren("text", BPMN2NS);
    System.out.print(texts.get(0).getText());

推荐阅读