You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用JDOM解析BPMN 2.0 XML文件获取<text>标签内容?

如何用JDOM获取BPMN 2.0 XML中<text>标签的文本内容

你已经成功定位到了<textAnnotation>元素,并且能获取到内部的<text>元素对象,接下来只需要调用JDOM Element类的getText()方法就能拿到标签里的文本内容啦!

完整代码示例

在你现有代码的基础上,添加遍历和文本提取的逻辑:

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);

// 遍历每个textAnnotation元素
for (Element annotation : textAnnotations) {
    // 获取内部的text子元素(必须指定BPMN2命名空间,否则会找不到元素)
    Element textElement = annotation.getChild("text", BPMN2NS);
    if (textElement != null) {
        // 获取原始文本内容
        String annotationText = textElement.getText();
        // 如果需要自动去除首尾空白字符,可改用这个方法
        // String annotationText = textElement.getTextTrim();
        System.out.println("Annotation content: " + annotationText);
    }
}

关键注意点

  • 务必在获取<text>子元素时指定BPMN2命名空间,因为你的XML里<text>属于BPMN 2.0标准命名空间下的元素,不指定命名空间会导致getChild()返回null。
  • getText()会返回标签内的全部文本(包括首尾的空白字符);如果需要清理文本两端的空格、换行符等,推荐使用getTextTrim()方法。

内容的提问来源于stack exchange,提问作者Elahe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:12:10