JAXB 元素映射 [英] JAXB Element mapping

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

问题描述

我有一个类似于以下内容的 xml:

I have an xml similar to:

    <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

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

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;
    }

}

演示

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);
    }

}

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;
    }

}

了解更多信息

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

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