Search code examples
jaxbmoxy

Getting Null value on complex type element using Moxy JAXB


This is my XML which I want to unmarshal (I do not have XSD for it). And I have to unmarshal it to get an array of names and Id's. So, names are getting generated just fine, but the issue is with the Id's. The Id's are set to null. Can anyone please help me identify the issue?

<?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>

This is my JAXB annotated class:

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;
        }
    }

This is my Java Class:

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(); }    

}  
}  

Now, this is the output I am getting:

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)

My issue is that why I am getting Svc ID as "null" when the same is working for Task Name.

Please note: 1. I am using MOXy for JAXB 2. Please note that when I change the sequence then names are getting set to null:

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


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

I am getting this output:

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)

Solution

  • Looks like mulltiple attributes on Moxy is not supported (What a shame :( I just started to love MOXy)

    Attached is a link

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