javaxmljacksonxmlmapper

XML Mapper to Pojo - Only Subelements?


i want to deserialize XML to a Java Pojo, but i dont need all elements and i want to avoid creating boilerplate-wrapper classes just to get some subelements

The XML looks like this

<a>
  <b>NotInterestingValue</b>
  <c>
    <d>InterestingValue</d> 
    <e>InterestingValue</e>
  </c>
</a>
I created A.class {

   String b;
   C c;
}


class C {
  String d;
  String e;
}

I created a XML-Mapper like this:

new XmlMapper(new JacksonXmlModule().readValue("xml-file", A.class)

--> This works. But is it possible to deserialize the XML only with C?

e.g. new XmlMapper(new JacksonXmlModule().readValue("xml-file", C.class) --> Doesnt work right now


Solution

  • I managed to find an answer: https://stackoverflow.com/a/41383076/15925714

    -->

    class C {
      String d;
      String e;
    
      @JsonProperty("c")
        private void unpackNameFromNestedObject(Map<String, String> c) {
            d= c.get("d");
            d= c.get("e");
        }
    }