注意: 我是 Eclipselink
JAXB(MOXy)的 负责人,并且是
JAXB(JSR-222) 专家组的成员。
使用任何JAXB(JSR-222)实现
使用任何JAXB(JSR-222)实现,您都可以使用
XmlAdapter来映射此用例。
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; }}
豆豆
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 /输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><alpha> <beta> <theta>abcd</theta> </beta></alpha>
使用Eclipselink JAXB(MOXy)
使用MOXy,您可以使用
@XmlPath扩展名来实现相同的映射:
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; }}
想要查询更多的信息
- 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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)