2

以下のXMLからtrkとその子要素のみをアンマーシャリングしたいと思います。ただし、マーシャリング中にルート要素としてgpxを作成したい

<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の外部メタデータは以下のように定義されています

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

以下のコード

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

この例外をスローしています

[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

何が欠けているのかわからない。

4

1 に答える 1

1

namespace要素に属性を指定するように設定する必要がありますxml-schema。これは、デフォルトでマッピングに適用される名前空間をMOXyに指示します。このxml-ns要素は、名前空間にプレフィックスを割り当てるために使用されます。

    <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

修正された外部マッピングドキュメントは次のようになります。

<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>
于 2012-08-20T18:01:56.600 に答える