Jess Farmer
Jess Farmer

Reputation: 11

Unmrashal nested element with JAXB

How to extract data in the nested elements of mentalHealthDays element ?

Here is my XML:

Here is my code:


    @SpringBootApplication

    public class DemoApplication {

        public static void main(String[] args) {

         try {

                File file = new File("C:\\Users\\JFarmer\\Projects\\demo\\xml_wife.xml");
                JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);

                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                Response customer = (Response) jaxbUnmarshaller.unmarshal(file);
                System.out.println(customer);

              } catch (JAXBException e) {
                e.printStackTrace();
              }


    }
    }
    @XmlRootElement(name="response")
    public class Response {

    String code;
    int fiscalYear;
    MentalHealthDays type;



    @Override
    public String toString() {
        return "Code=" + code + " , Fiscal Year=" + fiscalYear  + " ,test=" + type + "]";
    }

    @XmlElement
    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }

    @XmlElement
    public int getFiscalYear() {
        return fiscalYear;
    }


    public void setFiscalYear(int fiscalYear) {
        this.fiscalYear = fiscalYear;
    }

    @XmlElement
    public MentalHealthDays getType() {
        return type;
    }

    public void setType(MentalHealthDays type) {
        this.type = type;
    }

}

Also if you can provide any resources for better understanding, it'd be much appreciated.

I can turn the first couple of elements into variables in my Class, however, I can't figure out how to extract the data in the nested elements into variables. I also have multiple nested elements with the same name.

Upvotes: 0

Views: 67

Answers (1)

Cristian Colorado
Cristian Colorado

Reputation: 2040

You can create a List variable holding MentalHealthDays object, just make sure you name the attribute with the same name as the XMLElements you want to unsmarshall:

Response element

@XmlRootElement(name="response")
public class Response {
  private String code;
  private int fiscalYear;
  private List<MentalHealthDays> mentalHealthDays;

  @Override
  public String toString() {
    return "Response{" +
            "code='" + code + '\'' +
            ", fiscalYear=" + fiscalYear +
            ", mentalHealthDays=" + mentalHealthDays +
            '}';
  }

  @XmlElement
  public String getCode() {
    return code;
  }


  public void setCode(String code) {
    this.code = code;
  }

  @XmlElement
  public int getFiscalYear() {
    return fiscalYear;
  }


  public void setFiscalYear(int fiscalYear) {
    this.fiscalYear = fiscalYear;
  }

  @XmlElement
  public List<MentalHealthDays> getMentalHealthDays() {
    return mentalHealthDays;
  }

  public void setMentalHealthDays(List<MentalHealthDays> mentalHealthDays) {
    this.mentalHealthDays = mentalHealthDays;
  }
}

Inner class

public class MentalHealthDays {
  String type;
  int visitsAllowed;
  int visitsUser;

  public String getType() {
    return type;
  }

  public void setType(String type) {
    this.type = type;
  }

  public int getVisitsAllowed() {
    return visitsAllowed;
  }

  public void setVisitsAllowed(int visitsAllowed) {
    this.visitsAllowed = visitsAllowed;
  }

  public int getVisitsUser() {
    return visitsUser;
  }

  public void setVisitsUser(int visitsUser) {
    this.visitsUser = visitsUser;
  }

  @Override
  public String toString() {
    return "MentalHealthDays{" +
            "type='" + type + '\'' +
            ", visitsAllowed=" + visitsAllowed +
            ", visitsUser=" + visitsUser +
            '}';
  }
}

Upvotes: 1

Related Questions