views:

56

answers:

2
+1  Q: 

Java/XML problem

Hello all,

I'm having an issue generating XML via Java code. I have a class that for SailingAvailabilityResponseMessage. Within this class there are several other classes that are also used in the method I'm writing. Here is a snippet of what the SailingAvailabilityResponseMessage classes looks like:

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"type", "agency", "version", "release", "processingInfo", "advisoryMessage", "packageDescription", "sailingGroup", "registrationInformation"})
public class SailingAvailabilityResponseMessage {


 private ProcessingInfo processingInfo;
 private AdvisoryMessage advisoryMessage;
 private PackageDescription packageDescription;
 private SailingGroupResponse[] sailingGroup;
 private RegistrationInformation registrationInformation;


 String type;

 @XmlAttribute
 public String getType() {
  return type;
 }

The specific issue I'm encountering is related to the SailingGroupResponse[] sailingGroup; SailingGroupResponse[] inherits from sailingGroup. Here is what sailingGroup looks like:

public class SailingGroup {

 private SailingDescription sailingDescription;

 public SailingDescription getSailingDescription() {
  return sailingDescription;
 }

 public void setSailingDescription(SailingDescription sailingDescription) {
  this.sailingDescription = sailingDescription;
 }

And here is what SailingGroupResponse[] looks like:

import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder={"sailingInformation", "diningDetails", "modeOfTransportation", "currencyInfo", "packageDescription"})
public class SailingGroupResponse extends SailingGroup {

 private SailingInformation sailingInformation;
 private DiningDetails diningDetails;
 private ModeOfTransportation modeOfTransportation;
 private CurrencyInfo currencyInfo;
 private PackageDescription packageDescription;

The problem I'm encountering with the XML generation is that I'm expecting a tag of <sailingGroup> which I get but then I also get a tag of <sailingGroupResponse> which I do not want as it is not part of the original specs I'm following. I'm generating the XML via WebSphere RDi. I'm on SOAP 1.2 but the business partner I'm trying to develop for is on SOAP 1.1 so instead of being able to use JAX-WS runtime, I'm having to use JAX-RPC (apache AXIS will not allow me to generate).

Is anyone familiar with JAX-RPC runtime and issues similar to the one I'm asking? If anyone knows of a good site for me to visit for additional information, I'm willing to research further although so far I've not had much success in finding an answer.

Thanks,

Caren

+1  A: 

Introducing polymorphism into JAXB-annotated classes usually causes complications, as you're finding out.

Try this:

@XmlElement(type = SailingGroup.class)
public SailingGroupResponse[] getSailingGroup() {
    return sailingGroup;
}

And you may also need to add @XmlType to SailingGroup.

Mike Baranczak
A: 

Are you sure that JAXB is producing the sailingGroupResponse tag? When I run the following stripped down example I get reasonable XML:

XML Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <sailingGroup/>
    <sailingGroup/>
</root>

Demo Code

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;

public class Demo {

    public static void main(String[] args) throws Exception {

        SailingAvailabilityResponseMessage sarm = new SailingAvailabilityResponseMessage();
        SailingGroupResponse[] sgr = new SailingGroupResponse[2];
        sgr[0] = new SailingGroupResponse();
        sgr[1] = new SailingGroupResponse();
        sarm.setSailingGroup(sgr);

        JAXBElement<SailingAvailabilityResponseMessage> rootElement = new JAXBElement<SailingAvailabilityResponseMessage>(new QName("root"), SailingAvailabilityResponseMessage.class, sarm);

        JAXBContext jc = JAXBContext.newInstance(SailingAvailabilityResponseMessage.class, SailingGroup.class, SailingGroupResponse.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(rootElement, System.out);

    }

}

SailingAvailabilityResponseMessage

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(propOrder = { "type", "sailingGroup"})
public class SailingAvailabilityResponseMessage {

    private SailingGroupResponse[] sailingGroup;

    String type;

    public SailingGroupResponse[] getSailingGroup() {
        return sailingGroup;
    }

    public void setSailingGroup(SailingGroupResponse[] sailingGroup) {
        this.sailingGroup = sailingGroup;
    }

    @XmlAttribute
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

}

SalingGroupResponse

public class SailingGroupResponse extends SailingGroup {

}

SailingGroup

public class SailingGroup {

}
Blaise Doughan