ABOUTYOU
您可以使用适配器来做到这一点,但据我了解您的要求,这是没有必要的。如果您只是想解组为对象而不是 Map,则可以执行以下操作:从根开始:@XmlRootElement(name = "ACCOUNT_CHANGES")@XmlAccessorType(XmlAccessType.FIELD)public class AccountChanges { @XmlElement(name = "TYPE") private Type type; @XmlElement(name = "RECORD") private Record record;}让我们把 Type 去掉:@XmlAccessorType(XmlAccessType.FIELD)public class Type { @XmlAttribute private String value;}然后记录一下:@XmlAccessorType(XmlAccessType.FIELD)public class Record { @XmlElement(name = "SUBSCRIPTION") private Subscription subscription;}以及订阅:@XmlAccessorType(XmlAccessType.FIELD)public class Subscription { @XmlElement(name = "INFO") private List<Info> infoList;}信息将您的键作为属性,然后是一些值。它看起来像这样:@XmlAccessorType(XmlAccessType.FIELD)public class Info { @XmlAttribute private String key; @XmlValue private String value; public String getKey() { return key; } public String getValue() { return value; }}这将解组您的 xml,并且信息键和值将位于字段中。如果您想要映射中的键和值,可以使用适配器。适配器看起来像这样:public class MyMapAdapter extends XmlAdapter<Info, Map<String, String>> { private HashMap<String, String> hashMap = new HashMap<String, String>(); @Override public Map<String, String> unmarshal(Info v) throws Exception { hashMap.put(v.getKey(), v.getValue()); return hashMap; } @Override public Info marshal(Map<String, String> v) throws Exception { // do here actions for marshalling if u also marshal return null; }}您将更改订阅以使用适配器并将地图作为字段:@XmlAccessorType(XmlAccessType.FIELD)public class Subscription { @XmlElement(name = "INFO") @XmlJavaTypeAdapter(MyMapAdapter.class) private Map<String, String> infoMap;}有两种方法,都可以解组您的 xml 有效负载。