Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过JDOM解析xml文件来查找主属性的内容_Java_Xml_Bpmn_Jdom - Fatal编程技术网

Java 如何通过JDOM解析xml文件来查找主属性的内容

Java 如何通过JDOM解析xml文件来查找主属性的内容,java,xml,bpmn,jdom,Java,Xml,Bpmn,Jdom,我想解析我的xml文件BPMN2.0,以便通过JDOM读取标记的内容。我想读一下我的注解 <textAnnotation id="myAnnotation_ID" signavio:alignment="left" textFormat="text/plain"> <text>Test of myAnnotation</text> </textAnnotation> 这是我的密码: Document doc = new SAXB

我想解析我的xml文件BPMN2.0,以便通过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:],作为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());
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);
来自maven central:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>

它再次打印[元素:];text.get0返回一个jdom.Element,它是您的结果。jdom上可用的getText方法。元素返回字符串。。。
<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>