简体   繁体   中英

Getting null when converting xml into java object

Here is my code

class

  @Data
    @XmlRootElement(name = "ASX", namespace = "urn:synt:aps:zion")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Cpix {
        @XmlElement(name = "ZionicList")
        private ZionicList zionicList;
        @XmlElement(name = "Abrah")
        private AbrahList abrahList;
        
    }

rsp

<ns2:ASX xmlns="urn:djwfw:fwd2" xmlns:ns2="urn:synt:aps:zion">
  <ns2:ZionicList>
   ....
  </ns2:ZionicList>
  <ns2:Abrah>
   ....
  </ns2:Abrah>
</ns2:ASX>

Converter

public static <T> T convertXmlToObject(String response, Class<T> clazz) {
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        return (T) unmarshaller.unmarshal(new StringReader(response));
    } catch (JAXBException e) {
        fail(e.getMessage());
    }
    return null;
}

In the end Im getting null for ZionicList and AbrahList

It's ok when Im explicitly adding namespace to each xmlElement attribute

  @Data
    @XmlRootElement(name = "ASX", namespace = "urn:synt:aps:zion")
    @XmlAccessorType(XmlAccessType.FIELD)
    public class Cpix {
        @XmlElement(name = "ZionicList", namespace = "urn:synt:aps:zion")
        private ZionicList zionicList;
        @XmlElement(name = "Abrah", namespace = "urn:synt:aps:zion")
        private AbrahList abrahList;
        
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM