gpt4 book ai didi

java - 使用 JAXB 解码嵌套元素

转载 作者:行者123 更新时间:2023-12-02 10:09:51 24 4
gpt4 key购买 nike

如何提取 mentalHealthDays 元素的嵌套元素中的数据?

这是我的 XML:

这是我的代码:


@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;
}

}

此外,如果您可以提供任何资源以更好地理解,我们将不胜感激。

我可以将前几个元素转换为类中的变量,但是,我不知道如何将嵌套元素中的数据提取到变量中。我还有多个同名的嵌套元素。

最佳答案

您可以创建一个包含 MentalHealthDays 对象的 List 变量,只需确保您为该属性命名的名称与您想要的 XMLElements 相同解码:

响应元素

@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;
}
}

内部类

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

关于java - 使用 JAXB 解码嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55071720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com