使用Moxy JAXB在复杂类型元素上获取Null值

时间:2017-08-29 19:21:44

标签: jaxb moxy

这是我想要解组的XML(我没有XSD)。我必须解组它以获得一系列名称和Id。因此,名称生成得很好,但问题在于Id。 Id已设置为null。有人可以帮我确定一下这个问题吗?

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <bpmn:collaboration id="Collaboration_0b6zt1k" isClosed="false">
    <bpmn:participant id="Participant_113vq9r" processRef="CallActivity_00qe833" />
  </bpmn:collaboration>
  <bpmn:process id="CallActivity_00qe833" isClosed="false" isExecutable="true" name="Some Text" processType="None">
    <bpmn:serviceTask activiti:class="Assign PC" completionQuantity="1" id="ServiceTask_0ip6tj7" implementation="##WebService" isForCompensation="false" name="Some Text 1" startQuantity="1">
      <bpmn:extensionElements>
        <activiti:properties>
          <activiti:property name="specKey" value="ServiceTask_0ip6tj7" />
        </activiti:properties>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_0sa9y9o</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1bd3qmp</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask activiti:class="generateURL" completionQuantity="1" id="ServiceTask_11t11da" implementation="##WebService" isForCompensation="false" name="Some Text 2" startQuantity="1">
      <bpmn:extensionElements>
        <activiti:properties>
          <activiti:property name="specKey" value="ServiceTask_11t11da" />
        </activiti:properties>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_1bd3qmp</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0cynzzs</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask activiti:class="generateURL" completionQuantity="1" id="ServiceTask_11t11da" implementation="##WebService" isForCompensation="false" name="Some Text 3" startQuantity="1">
      <bpmn:extensionElements>
        <activiti:properties>
          <activiti:property name="specKey" value="ServiceTask_11t11da" />
        </activiti:properties>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_1bd3qmp</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0cynzzs</bpmn:outgoing>
    </bpmn:serviceTask>
  </bpmn:process>
</bpmn:definitions>

这是我的JAXB注释类:

package XMLToObject;

    import java.util.List;
    import javax.xml.bind.annotation.XmlAccessType;
    import javax.xml.bind.annotation.XmlAccessorType;
    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlRootElement;
    import org.eclipse.persistence.oxm.annotations.XmlPath;

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    public class definitions {

        @XmlPath("bpmn:process/bpmn:serviceTask/@name")
        @XmlAttribute
        List<String> name;

        @XmlPath("bpmn:process/bpmn:serviceTask/@id")
        @XmlAttribute
        List<String> id;

        @XmlAttribute
        String typeLanguage;

        public List<String> getname() {

            return name;
        }

        public List<String> getid() {

            return id;
        }

        public String gettypeLanguage() {
            return typeLanguage;
        }
    }

这是我的Java类:

package XMLToObject;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Gauravb
 */
import java.io.File;  
import java.util.Collection;
import java.util.List;
import javax.xml.bind.JAXBContext;  
import javax.xml.bind.JAXBException;  
import javax.xml.bind.Unmarshaller;  

public class XMLToObject {  
public static void main(String[] args) {  
     try {    
            File file = new File("employee.xml");    
            JAXBContext jaxbContext = JAXBContext.newInstance(definitions.class);    
            System.out.println("JAXB....."+jaxbContext.toString());  
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();    
            definitions p = (definitions) jaxbUnmarshaller.unmarshal(file);
            System.out.println("Task Name:"+p.getname());
            System.out.println("Svc ID:"+p.getid());
            System.out.println("Type Language:"+p.gettypeLanguage());
          } catch (JAXBException e) {e.printStackTrace(); }    

}  
}  

现在,这是我得到的输出:

run:
JAXB.....org.eclipse.persistence.jaxb.JAXBContext@18eed359
Task Name:[Some Text 1, Some Text 2, Some Text 3]
Svc ID:null
Type Language:http://www.w3.org/2001/XMLSchema
BUILD SUCCESSFUL (total time: 0 seconds)

我的问题是为什么我得到Svc ID为&#34; null&#34;当相同的任务名称。

请注意: 1.我正在使用MOXy进行JAXB 2.请注意,当我更改序列时,名称将设置为null:

@XmlPath("bpmn:process/bpmn:serviceTask/@id")
@XmlAttribute
List<String> id;


@XmlPath("bpmn:process/bpmn:serviceTask/@name")
@XmlAttribute
List<String> name;

我收到了这个输出:

run:
JAXB.....org.eclipse.persistence.jaxb.JAXBContext@18eed359
Task Name:null
Svc ID:[ServiceTask_0ip6tj7, ServiceTask_11t11da, ServiceTask_11t11da]
Type Language:http://www.w3.org/2001/XMLSchema
BUILD SUCCESSFUL (total time: 0 seconds)

1 个答案:

答案 0 :(得分:0)

看起来不支持Moxy上的mulltiple属性(真可惜:(我刚开始喜欢MOXy)

附件是一个链接

With MOXy and XPath, is it possible to unmarshal two lists of attributes?