Как проанализировать XML-файл по JDOM, чтобы найти содержимое основного атрибута - PullRequest
0 голосов
/ 30 мая 2018

Я хочу проанализировать мой 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"element.

Есть идеи, как мне прочитать" Test of myAnnotation "?

Ответы [ 2 ]

0 голосов
/ 23 июня 2018

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:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>
0 голосов
/ 30 мая 2018

Полагаю, что после того, как вы получите элемент 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());
...