JAXB convert anyelements to Map where element's name as key



I got the xml like :



<xml>
<A>1</A>
<B>awefq</B>
<X1>aaaa</X1>
<X2>qwr1</X2>
...
</xml>


where element name:X1,X2 are changed each time,but A and B is unchanged,I want to unmarshal this xml to a Java Bean like:



@XmlRootElement(name = "xml")
public class Message {

@XmlElement(name = "A")
private String a;

@XmlElement(name = "B")
private String b;

//any other elements here
@XmlAnyElement
private Map<String, String> attributes;
//... getters & setters
}


the converted attributes is {"X1":"aaaa","X2":"qwr1"}


And,I want to marshal this bean to xml too,How to do it in JAXB.


I've tried many ways,include the answer Bind multiple elements to map with attribute as key with java XML annotations, JAXB


No comments:

Post a Comment