MOXy XPath 역 정렬 화 요소가 null입니다.

Aug 18 2020

불필요한 래퍼 요소를 건너 뛰기 위해 XPath를 사용하여 camunda : property 요소를 List로 unmarshall하려고합니다. 불행히도 내 propertyList는 항상 null입니다. 이것은 태스크 클래스에 있습니다. 어떤 도움이라도 대단히 감사하겠습니다.

편집 # 1 : 불행히도 성공하지 못한 채 문제를 해결해야했던 다음 링크를 따라갔습니다. http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html공식 가이드입니다. 분명히 maven pom.xml 파일에는 몇 가지 문제가 있습니다. 나는 문제가 pom 파일 안에 있다고 생각합니다. 나는이 가이드를 따랐다https://www.javacodegeeks.com/2012/07/eclipselink-moxy-as-jaxb-provider.html 그러나 여전히 작동하지 않았습니다.

pom.xml 파일

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>BPMN-Marshaller</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <repositories><repository>
        <id>EclipseLink Repo</id>
        <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url>
        <name>EclipseLink Repo</name>
    </repository></repositories>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.moxy</artifactId>
            <version>3.0.0-M1</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.3</version>
        </dependency>
        <!-- Runtime, com.sun.xml.bind module -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>2.3.2</version>
        </dependency>
    </dependencies>

</project>

Java 클래스와 동일한 패키지 및 폴더에있는 jaxb.properties 파일 ( "프로젝트 구조"라는 이름의 첨부 이미지 참조)

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

package-info.java 파일

@XmlSchema(namespace="http://www.omg.org/spec/BPMN/20100524/MODEL", elementFormDefault=XmlNsForm.QUALIFIED, xmlns = {@XmlNs(prefix="bpmn", namespaceURI="http://www.omg.org/spec/BPMN/20100524/MODEL")
        ,@XmlNs(prefix = "camunda", namespaceURI = "http://camunda.org/schema/1.0/bpmn")})

package bpmn;

import javax.xml.bind.annotation.*;

xml 파일 스 니펫

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_13d3a6z" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.1.1">
  <bpmn:process id="Process_1tovjba" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>Flow_06i118e</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:task id="Activity_1d3friu" name="Task 1">
      <bpmn:extensionElements>
        <camunda:properties>
          <camunda:property name="start_date" value="01-04-2018" />
          <camunda:property name="duration" value="5" />
        </camunda:properties>
      </bpmn:extensionElements>
      <bpmn:incoming>Flow_06i118e</bpmn:incoming>
      <bpmn:outgoing>Flow_0linmbs</bpmn:outgoing>
    </bpmn:task>

정의 클래스

@XmlRootElement
public class Definitions {

 private String id;
 private Process process;



    public Definitions(){};
    public Definitions(String id, Process process){
        super();
        this.id = id;
        this.process = process;
    }

    @XmlAttribute
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement
    public Process getProcess() {
        return process;
    }

    public void setProcess(Process process) {
        this.process = process;
    }

    @Override
    public String toString(){
        return "Definitions [id23=" + id + ", process=23499999999999999" + process + "]";
    }

}

프로세스 클래스

public class Process {

    private String id;
    private List<Task> taskList;
    private List<SequenceFlow> sequenceFlowList;

    public Process(){};
    public Process(String id, List<Task> taskList, List<SequenceFlow> sequenceFlowList){
        super();
        this.id = id;
        this.taskList = taskList;
        this.sequenceFlowList = sequenceFlowList;
    }

    @XmlAttribute
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "task")
    public List<Task> getTaskList() {
        return taskList;
    }

    public void setTaskList(List<Task> taskList) {
        this.taskList = taskList;
    }

    @XmlElement(name = "sequenceFlow")
    public List<SequenceFlow> getSequenceFlowList() {
        return sequenceFlowList;
    }

    public void setSequenceFlowList(List<SequenceFlow> sequenceFlowList) {
        this.sequenceFlowList = sequenceFlowList;
    }
}

태스크 클래스

public class Task {

    private String id;
    private String name;
    private List<Property> propertyList;

    public Task(){}
    public Task(String id, String name, List<Property> propertyList){
        super();
        this.id = id;
        this.name = name;
        this.propertyList = propertyList;
    }

    @XmlAttribute
    @JsonProperty("text")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement(name = "property")
    @XmlPath("bpmn:extensionElements/camunda:properties/camunda:property")
    public List<Property> getPropertyList() {
        return propertyList;
    }

    public void setPropertyList(List<Property> propertyList) {
        this.propertyList = propertyList;
    }
}

속성 클래스

public class Property {

    private String name;
    private String value;

    public Property(){}
    public Property(String name, String value) {
        super();
        this.name = name;
        this.value = value;
    }

    @XmlAttribute
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute
    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

메인 클래스

public class XMLToObject {
    public static void main(String[] args) {

        try {

            File file = new File("process.bpmn");
            JAXBContext jaxbContext = JAXBContext.newInstance(Definitions.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Definitions definitions = (Definitions) jaxbUnmarshaller.unmarshal(file);
            System.out.println(definitions.getProcess().getTaskList().get(0).getPropertyList());

        } catch (JAXBException e) {
            e.printStackTrace();
        }


    }
}

프로젝트 구조

답변

1 andrewjames Aug 19 2020 at 00:36

귀하의 접근 방식을 다음과 같이 변경했으며 귀하 의 XML 파일에서 durationstart_date속성 데이터 에 액세스 할 수 있습니다.

그건 그렇고 OpenJDK 14를 사용하고 있습니다. 그러나이 접근 방식은 버전 8에서도 정상적으로 실행됩니다.

사용중인 POM에는 다음과 같은 종속성이 있습니다.

        <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!-- 
             Use 2.3.1 below to prevent "illegal 
             reflective access operation" warnings.
        -->
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.3.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.3.1</version>
        </dependency>

        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.7.6</version>
        </dependency>

(이 테스트를 위해 Jackson 종속성을 건너 뛰었습니다.)

또한 속성 파일을 처리하기 위해 내 POM 끝에 다음 섹션을 추가했습니다.

    <!-- to copy the jaxb.properties file to its class package: -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
    </build>

이렇게하면 속성 파일이 관련 클래스 파일과 함께 올바른 위치에 배포됩니다.

긍정적 인 확인으로 사용중인 JAXB 공급자를 확인하는 코드를 추가했습니다.

    private void checkProvider() throws JAXBException {
        JAXBContext jc = JAXBContext.newInstance(Definitions.class);

        String jaxbContextImpl = jc.getClass().getName();
        if(MOXY_JAXB_CONTEXT.equals(jaxbContextImpl)) {
            System.out.println("EclipseLink MOXy");
        } else if(METRO_JAXB_CONTEXT.equals(jaxbContextImpl)) {
            System.out.println("Metro");
        } else {
            System.out.println("Other");
        }
    }

최종 속성 값을 명시 적으로 인쇄하기 위해 속성 데이터를 반복하도록 코드를 수정했습니다.

List<Property> props = definitions.getProcess().getTaskList().get(0).getPropertyList();
props.forEach(prop -> {
    System.out.println(prop.getName() + " - " + prop.getValue());
});
//System.out.println(definitions.getProcess().getTaskList().get(0).getPropertyList());

결과 출력은 다음과 같습니다.

EclipseLink MOXy
start_date - 01-04-2018
duration - 5