java - 如何通过 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 测试”吗?
解决方案
我想一旦你得到你的 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());
推荐阅读
- javascript - TypeError:无法在 Object.addDishToCourse 读取未定义的属性“推送”
- python - 使用 GDB-Python Types API 查找嵌套结构的绝对偏移量
- json - 如何将 axios GET 响应数据保存为 json 或与到达的响应相同
- spring - 从数据库中获取 cron 表达式
- html - 保持一排按钮内联并带有溢出 x(在移动版本中)
- r - 当配对并不总是可能时,通常如何使用 combn ?
- android - onBackPressed 有时或总是在启动活动时自动调用
- r - 在 R 中计算向量中的重复项
- r - ggplot中的李克特堆积条形图与前后测试
- javascript - 使用 React slick 使幻灯片居中