1 回答
TA贡献2003条经验 获得超2个赞
以下是如何读取该 XML 的示例:
@XmlRootElement(name="Envelope", namespace="http://schemas.xmlsoap.org/soap/envelope/")
class Envelope {
@XmlElement(name="Body", namespace="http://schemas.xmlsoap.org/soap/envelope/")
Body body;
}
class Body {
@XmlElementWrapper(name="Features")
@XmlElement(name="Features")
List<Feature> features;
}
class Feature {
@XmlElement(name="id")
int id;
@XmlElement(name="code")
String code;
@XmlElement(name="Week")
String week;
}
String xml = "<SOAP-ENV:Envelope\r\n" +
"xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n" +
"<SOAP-ENV:Body>\r\n" +
" <Features>\r\n" +
" <Features>\r\n" +
" <id>213</id>\r\n" +
" <code>232BD13</code>\r\n" +
" <Week>202020</Week>\r\n" +
" </Features>\r\n" +
" </Features>\r\n" +
"</SOAP-ENV:Body>\r\n" +
"</SOAP-ENV:Envelope>";
Unmarshaller unmarshaller = JAXBContext.newInstance(Envelope.class).createUnmarshaller();
Envelope envelope = (Envelope) unmarshaller.unmarshal(new StringReader(xml));
for (Feature f : envelope.body.features)
System.out.printf("%d, %s, %s%n", f.id, f.code, f.week);
输出
213, 232BD13, 202020
为了简单起见,上面直接使用了字段,因此您可以看到发挥作用的注释。您的实际代码应该使用 getter 和 setter。
此外,命名空间可能应该在包级别处理。
添加回答
举报