2
votes

I would like to unmarshall only the trk and its child elements from the below XML. However, I want to create gpx as the root element while marshalling

<p:gpx creator="" version="1.1" xmlns:p="http://www.topografix.com/GPX/1/1"">
    <p:trk>
        <p:name>Test Track</p:name>
    </p:trk>
</p:gpx>

MOXy external meta-data is defined as below

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xml-accessor-type="PROPERTY" package-name="Debrief.Wrappers">

    <xml-schema element-form-default="QUALIFIED">
        <xml-ns prefix="p" namespace-uri="http://www.topografix.com/GPX/1/1" />
    </xml-schema>

    <java-types>
        <java-type name="TrackWrapper">
        <xml-root-element name="gpx"/>
            <java-attributes>
                <xml-attribute xml-path="trk/name/text()" java-attribute="name"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

The below code

Map<String, Object> props = new HashMap<String, Object>();
props.put(JAXBContextProperties.OXM_METADATA_SOURCE, this.getClass().getResourceAsStream("gpx-bindings.xml"));

jaxbContext = JAXBContext.newInstance("Debrief.Wrappers:Debrief.Wrappers.Track", TrackWrapper.class.getClassLoader(), props);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object unmarshal = unmarshaller.unmarshal(this.getClass().getResourceAsStream("gpx-data.xml"));

is throwing this exception

[Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element {http://www.topografix.com/GPX/1/1}gpx was not found in the project]
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.handleXMLMarshalException(JAXBUnmarshaller.java:956)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:529)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:149)
    at org.mwc.debrief.core.loaders.GPXLoader.doTheLoad(GPXLoader.java:70)
    at org.mwc.debrief.core.loaders.GPXLoader.main(GPXLoader.java:40)
Caused by: Exception [EclipseLink-25008] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.XMLMarshalException
Exception Description: A descriptor with default root element {http://www.topografix.com/GPX/1/1}gpx was not found in the project
    at org.eclipse.persistence.exceptions.XMLMarshalException.noDescriptorWithMatchingRootElement(XMLMarshalException.java:143)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshallerHandler.startElement(SAXUnmarshallerHandler.java:219)
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:111)
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:83)
    at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:72)
    at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:794)
    at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:660)
    at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:526)
    ... 3 more

Not sure what I am missing.

1

1 Answers

1
votes

You need to set the specify the namespace attribute in the xml-schema element. This tells MOXy what namespace should be applied to mappings by default. The xml-ns element is used to assign a prefix to the namespace.

    <xml-schema element-form-default="QUALIFIED" namespace="http://www.topografix.com/GPX/1/1">
        <xml-ns prefix="p" namespace-uri="http://www.topografix.com/GPX/1/1" />
    </xml-schema>

gpx-bindings.xml

Below is what the corrected external mapping document would look like:

<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xml-accessor-type="PROPERTY" package-name="Debrief.Wrappers">

    <xml-schema element-form-default="QUALIFIED" namespace="http://www.topografix.com/GPX/1/1">
        <xml-ns prefix="p" namespace-uri="http://www.topografix.com/GPX/1/1" />
    </xml-schema>

    <java-types>
        <java-type name="TrackWrapper">
        <xml-root-element name="gpx"/>
            <java-attributes>
                <xml-attribute xml-path="trk/name/text()" java-attribute="name"/>
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>