Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MOXy @XmlPath ignored

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

    }

}

like image 376
AgostinoX Avatar asked May 10 '11 10:05

AgostinoX


1 Answers

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:

  • http://bdoughan.blogspot.com/2011/05/specifying-eclipselink-moxy-as-your.html
  • http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html
  • http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html
like image 172
bdoughan Avatar answered Sep 20 '22 02:09

bdoughan