JAXB unmarshal XML elements to HashMap

前端 未结 2 1735
故里飘歌
故里飘歌 2021-01-21 14:20

I found a lot of articles that describe how to unmarshal a sequence of XML elements to a HashMap as long as they are within a \"parent\" element. However, I do not get this to w

2条回答
  •  余生分开走
    2021-01-21 15:06

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

    JAXB will treat each object relationship with a nesting relationship. Map is treated like an Object instead of a Collection so this is why you are getting the behaviour that you are seeing.

    MOXy has an XPath based mapping extension called @XmlPath that could be used for this use case.

    package com.foo.conf;
    
    import java.util.Map;
    
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlRootElement; 
    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    import org.eclipse.persistence.oxm.annotations.XmlPath;
    
    @XmlRootElement(name="checks")
    public class Checks {       
        @XmlJavaTypeAdapter(ChecksAdapter.class)
        @XmlPath(".")
        public Map checkMap;     
    }
    

    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

提交回复
热议问题