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> <p>You can use the external mapping file in EclipseLink JAXB (MOXy) to apply a second mapping to your object model, and the <code>@XmlPath</code> extension to flatten the object model.</p> <p><strong>External Mapping File (oxm.xml)</strong></p> <p>The following is what the external mapping file might look like if your model classes where in a package called <code>forum11007814</code>.</p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm" package-name="forum11007814" xml-accessor-type="FIELD"&gt; &lt;java-types&gt; &lt;java-type name="Customer"&gt; &lt;xml-root-element/&gt; &lt;java-attributes&gt; &lt;xml-element java-attribute="address" xml-path="."/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;java-type name="Address"&gt; &lt;java-attributes&gt; &lt;xml-element java-attribute="street" name="address_street"/&gt; &lt;xml-element java-attribute="city" name="address_city"/&gt; &lt;/java-attributes&gt; &lt;/java-type&gt; &lt;/java-types&gt; &lt;/xml-bindings&gt; </code></pre> <p><strong>Demo</strong></p> <p>Below is code that demonstrates how to leverage MOXy's external mapping file when creating the <code>JAXBContext</code>.</p> <pre><code>package forum11007814; import java.io.File; import java.util.*; import javax.xml.bind.*; import org.eclipse.persistence.jaxb.JAXBContextFactory; public class Demo { public static void main(String[] args) throws Exception { Map&lt;String, Object&gt; properties = new HashMap&lt;String,Object&gt;(1); properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum11007814/oxm.xml"); JAXBContext jc = JAXBContext.newInstance(new Class[] {Customer.class}, properties); File xml = new File("src/forum11007814/c.xml"); Unmarshaller unmarshaller = jc.createUnmarshaller(); Customer customer = (Customer) unmarshaller.unmarshal(xml); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(customer, System.out); } } </code></pre> <p><strong>jaxb.properties</strong></p> <p>To specify MOXy as your JAXB provider you need to add a file called <code>jaxb.properties</code> in the same package as your domain model with the following entry.</p> <pre><code>javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory </code></pre> <p><strong>c.xml/Output</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;customer&gt; &lt;firstName&gt;Juan&lt;/firstName&gt; &lt;lastName&gt;dela Cruz&lt;/lastName&gt; &lt;address_street&gt;123 Rizal Avenue&lt;/address_street&gt; &lt;address_city&gt;Manila&lt;/address_city&gt; &lt;/customer&gt; </code></pre>
    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. 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