我想要解析我的xml文件(BPMN2.0),以便通过JDOM读取<text>标记的内容。我的意思是读“myAnnotation的测试”
<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);但我已经能读懂的是作为"textAnnotation“元素的”[Element: <text [Namespace: http://www.omg.org/spec/BPMN/20100524/MODEL]/>],“的内容。
你知道怎么读“myAnnotation的测试”吗?
发布于 2018-05-30 22:33:48
我猜,一旦获得了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());发布于 2018-06-23 20:33:02
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>https://stackoverflow.com/questions/50605187
复制相似问题