Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

java - JAXB Element mapping

I have an xml similar to:

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


Using Any JAXB (JSR-222) Implementation

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

}

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

}

input.xml/Output

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

Using EclipseLink JAXB (MOXy)

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 More Information


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...