Using @XmlPath with jaxb/MOXy to map complex type

ε祈祈猫儿з 提交于 2019-12-01 00:54:08

When I ran your example everything worked fine. Since your real model probably has get/set methods you will need to ensure that you add @XmlAccessorType(XmlAccessType.FIELD) to your class otherwise MOXy (or any other JAXB impl) will also treat the corresponding properties as being mapped (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlPath("resource/information/date/text()")
    private String date;

    @XmlPath("resource/information/name/text()")
    private String name;

    @XmlPath("resource/elements/element")
    private List<Element> elements;

}

You also need to ensure that you have a jaxb.properties file in the same package as your domain model with the following entry to specify MOXy as your JAXB provider (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

For More Information


UPDATE #1

When your document is namespace qualified the @XmlPath annotation needs to factor this in. The nodes in the path can be qualified according to the @XmlSchema annotation.

package-info

In your package-info class the prefix s is assigned to the namespace URI http://www.example.eu/test.

@XmlSchema(
        namespace = "http://www.example.eu/test",
        attributeFormDefault = XmlNsForm.QUALIFIED,
        elementFormDefault = XmlNsForm.QUALIFIED,
        xmlns = {
    @XmlNs(
            prefix = "s",
            namespaceURI = "http://www.example.eu/test")
},
        location = "http://www.example.eu/test ResourceSchema.xsd")
package se.example.mavenproject1;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

Root

This means that the nodes qualified with the http://www.example.eu/test namespace should have the prefix s in the @XmlPath annotation.

import java.util.List;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlPath("s:resource/s:information/s:date/text()")
    private String date;

    @XmlPath("s:resource/s:information/s:name/text()")
    private String name;

    @XmlPath("s:resource/s:elements/s:element")
    private List<Element> elements;

}

UPDATE #2

So it seems as if the namespace needs to be specified in the @XmlPath when mapping the path to a complex object, but can be skipped when mapping the path to a simple object such as a String or an integer.

This is a bug. You should namespace qualify the @XmlPath for simple objects the same way you do for complex objects (see UPDATE #1). The correct mapping works today, we will fix the following bug so that the incorrect mapping behaves correctly. You can use the link below to track our progress on that issue:

So it seems as if the namespace needs to be specified in the @XmlPath when mapping the path to a complex object, but can be skipped when mapping the path to a simple object such as a String or an integer.

Original, incorrect, code

@XmlPath("resource/elements/refobj")
private List<RefObj> refObjs;

Updated, working code

@XmlPath("s:resource/s:elements/s:refobj")
private List<RefObj> refObjs;

When adding the namespace prefix to the @XmlPath all the objects are mapped as expected. If anyone could confirm this, and possibly explain why, I would be most happy to know the reason for this behavior.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!