JAXB元素映射 [英] JAXB Element mapping

查看:104
本文介绍了JAXB元素映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的xml类似于:

    <alpha>
            <beta>
                    <theta>abcd</theta>
            </beta>
    </alpha>

我想将 theta 元素映射到类MyBean中的属性 thetaValue

I want to map the theta element to a property thetaValue in the class MyBean

@XmlRootElement(name = "alpha")
public class MyBean {
    private String thetaValue;

    public String getThetaValue() {
        return this.thetaValue;
    }

    public void setThetaValue(String thetaValue) {
        this.thetaValue= thetaValue;
    }
}

有什么方法可以做到这一点吗?我正在使用JDK 1.6附带的jaxb

is there any way I can do this? I am using jaxb that comes with JDK 1.6

推荐答案

注意:我是 EclipseLink JAXB(MOXy) 领导和 JAXB(JSR-222) 专家组。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

使用任何JAXB(JSR-222)实施

使用任何JAXB(JSR-222)实现,您可以使用 XmlAdapter 来映射此用例。

Using any JAXB (JSR-222) implementation you could use an XmlAdapter to map this use case.

ThetaValueAdapter

ThetaValueAdapter

package forum9799081;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class ThetaValueAdapter extends XmlAdapter<ThetaValueAdapter.Beta, String> {

    @Override
    public Beta marshal(String string) throws Exception {
        Beta beta = new Beta();
        beta.theta = string;
        return beta;
    }

    @Override
    public String unmarshal(Beta beta) throws Exception {
        return beta.theta;
    }

    public static class Beta {
        public String theta;
    }

}

MyBean

MyBean

package forum9799081;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement(name = "alpha")
public class MyBean {
    private String thetaValue;

    @XmlElement(name="beta")
    @XmlJavaTypeAdapter(ThetaValueAdapter.class)
    public String getThetaValue() {
        return this.thetaValue;
    }

    public void setThetaValue(String thetaValue) {
        this.thetaValue = thetaValue;
    }

}

演示

Demo

package forum9799081;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(MyBean.class);

        File xml = new File("src/forum9799081/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        MyBean myBean = (MyBean) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(myBean, System.out);
    }

}

输入.xml /输出

input.xml/Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<alpha>
    <beta>
        <theta>abcd</theta>
    </beta>
</alpha>






使用EclipseLink JAXB(MOXy)

使用MOXy,您可以使用 @XmlPath 扩展来实现相同的映射:

Using MOXy you could use the @XmlPath extension to achieve the same mapping:

package forum9799081;

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

@XmlRootElement(name = "alpha")
public class MyBean {
    private String thetaValue;

    @XmlPath("beta/theta/text()")
    public String getThetaValue() {
        return this.thetaValue;
    }

    public void setThetaValue(String thetaValue) {
        this.thetaValue = thetaValue;
    }

}

For更多信息

For More Information

  • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
  • http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html

这篇关于JAXB元素映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆