我正在尝试获取 xml 文件的值。
<p1>
<cts>Pq44</cts>
<cts>qw44</cts>
</p1>
P1.JAVA
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class P1 {
private List<Cts> cts;
public P1() {
cts = new ArrayList<cts>();
}
public List<cts> getcts() {
return cts;
}
public void setcts(List<cts> cts) {
this.cts = cts;
}
}
CTS.JAVA
public class CTS {
private String ct;
// Getter and setter for ct.
}
我的Main.java
try {
File file = new File("D:\\Bye.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(P1.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
P1 p = (P1) jaxbUnmarshaller.unmarshal(file);
List<CTS> cts = p.getCTS();
// Size of list coming right `2`
for (CTS c : cts) {
System.out.println(CTS2.getCT());
}
} catch (JAXBException e) {
e.printStackTrace();
}
当我运行 main.java 时,它会打印:
null
null