Search code examples
xmljaxbmarshalling

JAXB marshalled xml output in wrong case and missing some elements


Using JAXB 3.0, Java 1.11, Eclipse 2019-12. Values in the xml object are set with the setter methods, and when outputting the xml it looks like this

<?xml version="1.0" encoding="UTF-8"?>
 <USERID>xxxxxxxxxxxx</USERID>
 <clientIp>122.3.3</clientIp>
 <revision>1</revision>
 <sourceId>xxxxxxxxxx</sourceId>

instead of

<?xml version="1.0" encoding="UTF-8"?>
<TrackFieldRequest USERID="xxxxxxxxxxx">
<Revision>1</Revision>
<ClientIp>122.3.3</ClientIp>
<SourceId>xxxxxxxxxx</SourceId>
<TrackID ID="xxxxxxxxxxxxxxxxxxxxxx"/>
<TrackID ID="xxxxxxxxxxxxxxxxxxxxxx"/>
</TrackFieldRequest>

Here's a snippet of the marshal code

TrackFieldRequest request = tracker.buildRequest();
                
        try {
        
           javax.xml.bind.JAXBContext    jaxbContext = javax.xml.bind.JAXBContext.newInstance(TrackFieldRequest.class);
           
           //Create Marshaller
           Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

           //Required formatting??
           jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

           //Print XML String to Console
           StringWriter sw = new StringWriter();
            
           //Write XML to StringWriter
           jaxbMarshaller.marshal(request, sw);
            
           //Verify XML Content
           String xmlContent = sw.toString();
           System.out.println( xmlContent );

And here's part of the annotated jaxb class TrackFieldRequest

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TrackFieldRequest", propOrder = {
    "Revision",
    "ClientIp",
    "SourceId",
    "TrackID"
})
@XmlRootElement(name = "TrackFieldRequest")
public class TrackFieldRequest {

    @XmlElement(name = "Revision", required = true)
    protected BigInteger Revision;
    @XmlElement(name = "ClientIp", required = true)
    protected String ClientIp;
    @XmlElement(name = "SourceId", required = true)
    protected String SourceId;
    @XmlElement(name = "TrackID", required = true)
    protected List<TrackFieldRequest.TrackID> TrackID;
    @XmlAttribute(name = "USERID")
    protected String userid;
:
:
    public static class TrackID {

        @XmlAttribute(name = "ID")
        protected String id;

Double-checked the contents of the two tracking id's and they're present System.out.println(request.getTrackID().get(0).getID()); System.out.println(request.getTrackID().get(1).getID());

Any ideas why the multiple-occurs track id's are missing, the root element is missing, and the cases of the element names are incorrect?


Solution

  • Going to jakarta worked. Got rid of all the jaxb jars and kept the jakarta jar jakarta.xml.bind-api-3.0.0 and redid the import statement

    import jakarta.xml.bind.*;

    and updated jaxb.properties

    jakarta.xml.bind.JAXBContextFactory=org.eclipse.persistence.jaxb.JAXBContextFactory