Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Note:</strong> I'm the <a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow"><strong>EclipseLink JAXB (MOXy)</strong></a> lead and a member of the <a href="http://jcp.org/en/jsr/detail?id=222" rel="nofollow"><strong>JAXB (JSR-222)</strong></a> expert group.</p> <hr/> <p><strong>Using Any JAXB (JSR-222) Implementation</strong></p> <p>Using any JAXB (JSR-222) implementation you could use an <code>XmlAdapter</code> to map this use case.</p> <p><em><strong>ThetaValueAdapter</em></strong></p> <pre><code>package forum9799081; import javax.xml.bind.annotation.adapters.XmlAdapter; public class ThetaValueAdapter extends XmlAdapter&lt;ThetaValueAdapter.Beta, String&gt; { @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; } } </code></pre> <p><em><strong>MyBean</em></strong></p> <pre><code>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; } } </code></pre> <p><em><strong>Demo</em></strong></p> <pre><code>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); } } </code></pre> <p><em><strong>input.xml/Output</em></strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;alpha&gt; &lt;beta&gt; &lt;theta&gt;abcd&lt;/theta&gt; &lt;/beta&gt; &lt;/alpha&gt; </code></pre> <hr/> <p><strong>Using EclipseLink JAXB (MOXy)</strong></p> <p>Using MOXy you could use the <code>@XmlPath</code> extension to achieve the same mapping:</p> <pre><code>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; } } </code></pre> <p><em><strong>For More Information</em></strong></p> <ul> <li><a href="http://blog.bdoughan.com/2010/07/xpath-based-mapping.html" rel="nofollow">http://blog.bdoughan.com/2010/07/xpath-based-mapping.html</a></li> <li><a href="http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html" rel="nofollow">http://blog.bdoughan.com/2011/09/mapping-objects-to-multiple-xml-schemas.html</a></li> <li><a href="http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html" rel="nofollow">http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload