Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I always recommend that people design the best model possible for their application. Then let JAXB handle the difficulty of mapping it to XML. In this case if it makes sense for parent to have a Map of children then by all means model it this way. JAXB does have some support for Map, but with the XML representation you describe you will need to use an XmlAdapter. For more information see:</p> <ul> <li><a href="http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html" rel="noreferrer">http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html</a></li> </ul> <p>All of the Pros seem reasonable. None of the cons are valid:</p> <ul> <li>As you can see from the above example when using @XmlJavaTypeAdaper your property is NOT restricted to a concrete implementation of Map.</li> <li>The XmlAdapter is external to the POJO model, so the model does not have the list to/from map behaviour.</li> <li>XmlAdapter is meant to be a catch all for any hard to map use case. It is not restricted to only simple objects. A common use case is for mapping instances of Map.</li> </ul> <p>If you want to eliminate the wrapper element, then you can use the @XmlPath extension in the MOXy JAXB implementation. The Foo class from my blog article would be modified slightly:</p> <pre><code>import java.util.HashMap; import java.util.Map; import javax.xml.bind.annotation.*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.eclipse.persistence.oxm.annotations.XmlPath; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Foo { @XmlJavaTypeAdapter(MyMapAdapter.class) @XmlPath(".") Map&lt;Integer, String&gt; map = new HashMap&lt;Integer, String&gt;(); public Map&lt;Integer, String&gt; getMap() { return map; } public void setMap(Map&lt;Integer, String&gt; map) { this.map = map; } } </code></pre> <p>For more information see:</p> <ul> <li><a href="http://bdoughan.blogspot.com/2010/07/xpath-based-mapping.html" rel="noreferrer">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="noreferrer">http://bdoughan.blogspot.com/2010/09/xpath-based-mapping-geocode-example.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.
    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