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