jaxb - JAXB (MOXy) 中 XML 的部分映射

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

不知道我错过了什么。

最佳答案

您需要在 xml-schema 元素中设置指定 namespace 属性。这告诉 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>

关于jaxb - JAXB (MOXy) 中 XML 的部分映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031148/

相关文章:

java - 为原语自定义 jaxb 绑定(bind)

java - JAXB 解码 @XmlAnyElement

xml - JAXB(在 Jersey JAX-RS 中)是否有可能支持 java.util.HashMap 的 XML 和 JSON 格式

java - 通过 JAXB 为枚举提供自定义值序列化

jpa - eclipselink 查询结果缓存不起作用

java - JPA EclipseLink 2 查询性能

java - 使用 eclipselink 和 java.sql 的 JPA : when connect to DB

java - JAXB 通过映射

java - JAXB - 解码 XML 文件

java - 响应中没有 namespace 的 SOAP(Spring Boot)