这是我的bean类
class resp {
String code;
String msg;
resp() {
code = null;
msg = null;
}
resp(String code, String msg) {
this.code = code;
this.msg = msg;
}
@XmlAttribute(name = "code")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@XmlAttribute(name = "msg")
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
此类包含resp类列表
@XmlRootElement(name = "resps")
class resps {
List<resp> resp = null;
@XmlElement
public List<resp> getResp() {
return resp;
}
public void setResp(List<resp> resp) {
this.resp = resp;
}
}
主要方法:我使用jaxb将resps.xml解组为java对象(resps)
public static void main(String args[]) throws JAXBException {
try {
File file = new File("resps.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(resps.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
resps res = (resps) jaxbUnmarshaller.unmarshal(file);
List<resp> list = res.getResp();
int i = 1;
for (resp ans : list){
System.out.println(" record " + i++ + " contents :" + ans.code
+ " " + ans.msg);
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
----------------------------- resps.xml --------------- ------------------
<?xml version="1.0" encoding="UTF-8"?>
<resps>
<resp>
<code>testCode1</code>
<msg>testMsg1</msg>
</resp>
<resp>
<code>testCode2</code>
<msg>testMsg2</msg>
</resp>
</resps>
答案 0 :(得分:1)
在resp.class中,使用@XmlAttribute注释代码和msg getter方法,但在xml中,这些方法作为元素出现。将注释@XmlAttribute更改为@XmlElement。
class resp {
String code;
String msg;
resp() {
code = null;
msg = null;
}
resp(String code, String msg) {
this.code = code;
this.msg = msg;
}
@XmlElement(name = "code")
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@XmlElement(name = "msg")
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
如果您的xml如下所示,请使用@XmlAttribute:
<?xml version="1.0" encoding="UTF-8"?>
<resps>
<resp code="testCode1" msg = "testMsg1"/>
<resp code="testCode2" msg = "testMsg2"/>
</resps>
答案 1 :(得分:0)
更改您的resp.java代码,如下所示
@HostListener('window:resize')
resizeWindow() {
// window has been resized
}
我建议你应该参考一些教程,关于什么是XmlAttribute和XmlElement