5

I have a very simple class with two fields, String sourceAddress and int port.
I want them to be mapped on the source/address and source/port nodes instead of the jaxb default sourceAddress and sourcePort.
So i use MOXy @XmlPath annotation.
The problem is that the annotation is just ignored and i get the "jaxb default" xml file:

<szk>
    <sourceAddress>test</sourceAddress>
    <sourcePort>10000</sourcePort>
</sz>

thanks in advance for any help Agostino

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.jaxb.JAXBContext;
import org.eclipse.persistence.oxm.annotations.XmlPath;

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

    @XmlPath("source/address")
    private String sourceAddress;
    @XmlPath("source/port")
    private int sourcePort;

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

        SZK k = new SZK();
        k.sourceAddress = "test";
        k.sourcePort = 10000;

        javax.xml.bind.JAXBContext jc = JAXBContext.newInstance(SZK.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(k, System.out);

    }

}

bdoughan
  • 147,609
  • 23
  • 300
  • 400
AgostinoX
  • 7,477
  • 20
  • 77
  • 137
  • Hi, Thanks for this question. actually, I am also trying to do something similar but for some reason, it's not working as expected. I tried the things mentioned in this answer as well. I tried this approach and the example from the authors blog but still the `JAXB Marshalling` does not work as expected with `@XmlPath`. Seems like the XML produced with and without `@XmlPath` are the same. Can you please once look into this example and provide your answer: https://stackoverflow.com/questions/67500565/xmlpath-has-no-impact-during-the-jaxb-marshalling – BATMAN_2008 May 12 '21 at 10:26

1 Answers1

8

The most likely cause of this issue is that you are missing the jaxb.properties file to specify that EclipseLink MOXy should be used as the JAXB provider. The jaxb.properties file must be placed in the same package as your domain model and contain the following entry:

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

For More Information:

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 1
    great honor being answered directly from "the source" :-). Yes, it was the jaxb.properties file missing. I performed some more test on these files, to confirm that I can have a different provider each package. Ok till i tried to marshal classes from different packages with JAXBContext.newInstance(pkg1.C1.class, pkg2.C2.class)... but this is a pretty advanced issue. – AgostinoX May 10 '11 at 22:46
  • 5
    If you are using maven its worth checking your target/classes.... on the file system for the properties file too. Just in case maven is setup not to copy the file into target – Derek Nov 28 '13 at 11:33
  • @Derek this comment should be highlighted, I wasted 2 days of mine and the solution was hiding in these two lines. Maven was simply excluding the properties file in the target. Make sure jaxb.properties is still there after maven compile. – a_a Mar 18 '19 at 13:12
  • Regarding Derek's note: In my case, it wasn't just Maven, it was also my IDE. A [facepalm](https://stackoverflow.com/questions/39388961/not-able-to-configure-moxy-using-jaxb) moment for me. – andrewJames Feb 18 '20 at 22:08
  • @bdoughan Hi, Thanks for this question. actually, I am also trying to do something similar but for some reason, it's not working as expected. I tried the things mentioned in this answer as well. I tried this approach and the example from the authors blog but still the `JAXB Marshalling` does not work as expected with `@XmlPath`. Seems like the XML produced with and without `@XmlPath` are the same. Can you please once look into this example and provide your answer: https://stackoverflow.com/questions/67500565/xmlpath-has-no-impact-during-the-jaxb-marshalling – BATMAN_2008 May 12 '21 at 10:26