首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何利用JDOM解析xml文件以找到主属性的内容

如何利用JDOM解析xml文件以找到主属性的内容
EN

Stack Overflow用户
提问于 2018-05-30 20:50:32
回答 2查看 171关注 0票数 0

我想要解析我的xml文件(BPMN2.0),以便通过JDOM读取<text>标记的内容。我的意思是读“myAnnotation的测试”

代码语言:javascript
复制
  <textAnnotation id="myAnnotation_ID" signavio:alignment="left" textFormat="text/plain">
     <text>Test of myAnnotation</text>
  </textAnnotation>

下面是我的代码:

代码语言:javascript
复制
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的测试”吗?

EN

回答 2

Stack Overflow用户

发布于 2018-05-30 22:33:48

我猜,一旦获得了textAnnotation元素,就只需获得所有名为" text“的子元素,并使用以下代码获取其中的文本。

代码语言:javascript
复制
    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());
票数 1
EN

Stack Overflow用户

发布于 2018-06-23 20:33:02

SimpleXml可以做到:

代码语言:javascript
复制
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);

将输出:

代码语言:javascript
复制
Test of myAnnotation

来自maven central:

代码语言:javascript
复制
<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50605187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档