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。
此外,命名空間可能應該在包級別處理。
添加回答
舉報