如何通过 JDOM 解析 xml 文件以查找主属性的内容



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

<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"元素的"内容"。

知道我如何阅读"我的注释测试"吗?

我想一旦你得到了你的textAnnotation Element,你只需要让所有名为"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());

SimpleXml 可以做到:

final String data = "<textAnnotation id="myAnnotation_ID" signavio:alignment="left" textFormat="text/plain">n" +
"     <text>Test of myAnnotation</text>n" +
"  </textAnnotation>";
final SimpleXml simple = new SimpleXml();
final Element element = simple.fromXml(data);
System.out.println(element.children.get(0).text);

将输出:

Test of myAnnotation

从 maven central:

<dependency>
<groupId>com.github.codemonstur</groupId>
<artifactId>simplexml</artifactId>
<version>1.4.0</version>
</dependency>

最新更新