Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Below are a couple of approaches:</p> <p><strong>Standard JAXB</strong></p> <p>The following will work with any JAXB implementation (Metro, <a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow">MOXy</a>, JaxMe, etc). You could create the element "<em>FOO</em>", and then marshal your instance of Bar into it. The code below how to demonstrate this using StAX (it could also be done with DOM or SAX):</p> <pre><code>import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.stream.XMLOutputFactory; import javax.xml.stream.XMLStreamWriter; public class Demo { public static void main(String[] args) throws Exception { XMLOutputFactory xof = XMLOutputFactory.newFactory(); XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out); xsw.writeStartDocument(); xsw.writeStartElement("FOO"); Bar bar = new Bar(); bar.setId(1); bar.setPoints(111); bar.setCode(123); JAXBContext jc = JAXBContext.newInstance(Bar.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.marshal(bar, xsw); xsw.writeEndElement(); xsw.writeEndDocument(); xsw.close(); } } </code></pre> <p><strong>EclipseLink JAXB (MOXy)</strong></p> <p>If you happen to be using <a href="http://www.eclipse.org/eclipselink/moxy.php" rel="nofollow">EclipseLink JAXB (MOXy)</a>, then you can use the @XmlPath extension (I'm the MOXy tech lead). Your Bar class would look like:</p> <pre><code>import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement(name="FOO") @XmlType(propOrder={"id", "points", "code"}) public class Bar { private int id; private int points; private int code; @XmlPath("BAR/id/text()") public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlPath("BAR/POINTS/text()") public int getPoints() { return points; } public void setPoints(int points) { this.points = points; } @XmlPath("BAR/CODE/text()") public int getCode() { return code; } public void setCode(int code) { this.code = code; } } </code></pre> <p>For more information see:</p> <ul> <li><a href="http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html" rel="nofollow">http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html</a></li> <li><a href="http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html" rel="nofollow">http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.html</a></li> <li><a href="http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html" rel="nofollow">http://bdoughan.blogspot.com/2011/03/map-to-element-based-on-attribute-value.html</a></li> </ul>
    singulars
    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.
 

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