Search code examples
jaxbeclipselinkxml-namespacesmoxy

How to bind element of namespace with EclipseLink MOXy?


I have the following java model:

public class Container {
    public X x;
    public Y y;
}
public class X {
    public String test;
}
public class Y {}

and want to map the following document:

<?xml version="1.0" encoding="UTF-8"?>
<my:root xmlns:my="http://my.url" test="value">
    <x/>
</my:root>

For this, I use a bindings file:

<?xml version="1.0"?>
<xml-bindings
    xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    package-name="example">
    <xml-schema namespace="http://my.url" />
    <java-types>
        <java-type name="Container">
            <xml-root-element name="root"/>
            <xml-type namespace="http://my.url" />
            <java-attributes>
                <xml-element java-attribute="x" xml-path="x" type="example.X"/>
                <xml-element java-attribute="y" xml-path="." type="example.Y"/> 
            </java-attributes>
        </java-type>
        <java-type name="Y">
            <java-attributes>
                <xml-element java-attribute="test" xml-path="@test"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

It fails with an Exception:

[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element {http://my.url}root was not found in the project]
    at org.eclipse.persistence.jaxb.JAXBBinder.unmarshal(JAXBBinder.java:100)
    at example.Main.main(Main.java:39)

The model for the given document is not necessarily very intuitive but neither do I have an influence on the model or the document.

How can I make it work? Everything worked fine until the namespace "http://my.url" was introduced.


Solution

  • Using the information you provided in your question, the following demo code works for me:

    Demo Code

    Demo

    import java.io.File;
    import java.util.*;
    import javax.xml.bind.*;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "example/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Container.class}, properties);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/example/input.xml");
            Object result = unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(result, System.out);
        }
    
    }
    

    Output

    <?xml version="1.0" encoding="UTF-8"?>
    <ns0:root xmlns:ns0="http://my.url">
       <x/>
    </ns0:root>