I have a restful web service that return a collection of employees but i don't know how to process this returned data. I know if it were to be a single object that was returned i could unmarshal it and cast to my desire class but how do i do that and save my data in a list, arraylist, set or any other form of collection.
//single object (perfect)
Customer c = (Customer) jc.createUnmarshaller().unmarshal(xml);
// Error
List<Customer> list = (List<Customer>) jc.createUnmarshaller().unmarshal(xml);
Thanks.
Try
@XmlRootElement
class Customer {
@XmlElement
String name;
public String toString() {
return name;
}
}
@XmlRootElement
class Customers {
@XmlElement(name = "customer")
List<Customer> list;
}
class Test {
public static void main(String[] args) throws Exception {
String xml = "<customers><customer><name>Ayodeji</name></customer></customers>";
JAXBContext ctx = JAXBContext.newInstance(Customers.class);
Unmarshaller um = ctx.createUnmarshaller();
Customers res = (Customers)um.unmarshal(new StringReader(xml));
System.out.println(res.list);
}
}
output
[Ayodeji]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.