JAXB XML 适配器通过注释工作,但不通过 setAdapter

2024-04-19

我完全了解如何使用XML适配器 http://download.oracle.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html to 转换不可映射的类型 http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html,或者只是更改某些对象序列化/反序列化为 XML 的方式。如果我使用注释(无论是包级别还是其他级别),一切都会很好。问题是我试图更改第三方对象的表示,而我无法更改源代码(即为了注入注释)。

考虑到 Marshaller 对象有一个方法,这应该不是问题手动添加适配器 http://download.oracle.com/javase/6/docs/api/javax/xml/bind/Marshaller.html#setAdapter%28javax.xml.bind.annotation.adapters.XmlAdapter%29。不幸的是,无论我做什么,我都无法以这种方式设置适配器来“启动”。例如,我有一个类表示 XYZ 空间(地心坐标)中的一个点。在我生成的 XML 中,我希望将其转换为纬度/经度/高度(大地坐标)。这是我的课程:

地心

package testJaxb;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class GeocentricCoordinate {
    // Units are in meters; see http://en.wikipedia.org/wiki/Geocentric_coordinates
    private double x;
    private double y;
    private double z;

    @XmlAttribute
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    @XmlAttribute
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    @XmlAttribute
    public double getZ() {
        return z;
    }
    public void setZ(double z) {
        this.z = z;
    }
}

Geodetic

package testJaxb;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
/**
 * @see http://en.wikipedia.org/wiki/Geodetic_system
 */
@XmlRootElement
public class GeodeticCoordinate {

    private double latitude;
    private double longitude;
    // Meters
    private double altitude;

    public GeodeticCoordinate() {
        this(0,0,0);
    }

    public GeodeticCoordinate(double latitude, double longitude, double altitude) {
        super();
        this.latitude = latitude;
        this.longitude = longitude;
        this.altitude = altitude;
    }

    @XmlAttribute
    public double getLatitude() {
        return latitude;
    }
    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    @XmlAttribute
    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    @XmlAttribute
    public double getAltitude() {
        return altitude;
    }
    public void setAltitude(double altitude) {
        this.altitude = altitude;
    }



}

地心转大地位置适配器

package testJaxb;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;


/**
 * One of our systems uses xyz coordinates to represent locations. Consumers of our XML would much
 * prefer lat/lon/altitude.  This handles converting between xyz and lat lon alt.  
 * 
 * @author ndunn
 *
 */
public class GeocentricToGeodeticLocationAdapter extends XmlAdapter<GeodeticCoordinate,GeocentricCoordinate> {

    @Override
    public GeodeticCoordinate marshal(GeocentricCoordinate arg0) throws Exception {
        // TODO: do a real coordinate transformation
        GeodeticCoordinate coordinate = new GeodeticCoordinate();
        coordinate.setLatitude(45);
        coordinate.setLongitude(45);
        coordinate.setAltitude(1000);
        return coordinate;
    }

    @Override
    public GeocentricCoordinate unmarshal(GeodeticCoordinate arg0) throws Exception {
        // TODO do a real coordinate transformation
        GeocentricCoordinate gcc = new GeocentricCoordinate();
        gcc.setX(100);
        gcc.setY(200);
        gcc.setZ(300);
        return gcc;
    }
}

对象与位置字段

package testJaxb; 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class ObjectWithLocation {

    private GeocentricCoordinate location = new GeocentricCoordinate();

    public GeocentricCoordinate getLocation() {
        return location;
    }

    public void setLocation(GeocentricCoordinate location) {
        this.location = location;
    }


    public static void main(String[] args) {

        ObjectWithLocation object = new ObjectWithLocation();

        try {
            JAXBContext context = JAXBContext.newInstance(ObjectWithLocation.class, GeodeticCoordinate.class, GeocentricCoordinate.class);
            Marshaller marshaller = context.createMarshaller();

            marshaller.setAdapter(new GeocentricToGeodeticLocationAdapter());
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            marshaller.marshal(object, System.out);

        }
        catch (JAXBException jaxb) {
            jaxb.printStackTrace();
        }
    }
}

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectWithLocation>
    <location z="0.0" y="0.0" x="0.0"/>
</objectWithLocation>

通过使用注释(在我的package-info.java file):

@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters
({
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=GeocentricToGeodeticLocationAdapter.class,type=GeocentricCoordinate.class),
})

package package testJaxb;

我得到以下(所需的)xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<objectWithLocation>
    <location longitude="45.0" latitude="45.0" altitude="1000.0"/>
</objectWithLocation>

所以我的问题是双重的。

  1. 为什么适配器在带注释时可以工作,但在通过显式设置时却不能工作setAdapter method?
  2. 当我有无法注释的类并且无法修改其 package-info.java 以添加注释时,如何解决此问题?

The setAdapter(XmlAdapter) on Marshaller用于为已使用注释的属性传入已初始化的 XmlAdapter@XmlJavaTypeAdapter。下面的链接是我利用这种行为的答案:

  • 使用 JAXB 交叉引用两个 XML 文件中的 XmlID https://stackoverflow.com/questions/5319024/using-jaxb-to-cross-reference-xmlids-from-two-xml-files/5327425#5327425

如果你想映射第三方类,你可以使用EclipseLink JAXB (MOXy) http://www.eclipse.org/eclipselink/moxy.php的 XML 映射文件(我是 MOXy 的负责人):

  • http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html http://bdoughan.blogspot.com/2010/12/extending-jaxb-representing-annotations.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JAXB XML 适配器通过注释工作,但不通过 setAdapter 的相关文章

随机推荐