简体   繁体   English

使用JAXB的无误嵌套元素

[英]Unmrashal nested element with JAXB

How to extract data in the nested elements of mentalHealthDays element ? 如何在mentalHealthDays元素的嵌套元素中提取数据?

Here is my XML: 这是我的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. 我可以将前两个元素转换为Class中的变量,但是,我不知道如何将嵌套元素中的数据提取为变量。 I also have multiple nested elements with the same name. 我也有多个同名的嵌套元素。

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: 您可以创建一个MentalHealthDays对象的List变量,只需确保使用与要取消编组的XMLElements相同的名称来命名该属性:

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 +
            '}';
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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