JAXB中的XML部分映射(MOXy)

时间:2012-08-20 00:48:14

标签: jaxb eclipselink jaxb2 moxy

我想从以下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

不确定我错过了什么。

1 个答案:

答案 0 :(得分: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>