JAXB: How to suppress surrounding XmlElement when using XmlJavaTypeAdapter?

前端 未结 1 783
北荒
北荒 2021-01-23 09:45

I use @XmlJavaTypeAdapter to transform Map objects into List when marshalling (and vice versa when u

相关标签:
1条回答
  • 2021-01-23 10:18

    Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

    You could use the @XmlPath(".") extension in MOXy to map this use case. Specifying "." as the path indicates that the contents of the child should be written into the parent objects element.

    import java.util.HashMap;
    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(MyMapItemAdapter.class)
        @XmlPath(".")
        Map<String, MapItem> map = new TreeMap<String, MapItem>();       
    
    }
    

    Full Example

    • JAXB marshal an ArrayList created by XmlAdapter

    For More Information

    • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    0 讨论(0)
提交回复
热议问题