hi,
I'll eventually figure this out, but i have been struggling for at least 2
days trying to unmarshall the simplest xml.. i must really be getting
frustrated if i resorted to emailing the open source developers.. anyway:
all i want to do is unmarshall a simple xml file involving nested
collections
<persons>
<person><name>billy</name>
<address>
<address1>West St.</address1>
</address>
<address>
<address1>East St.<address1>
</address>
</person>
<person><name>bob</name>
<address>
<address1>North St.</address1>
</address>
<address>
<address1>South St.<address1>
</address>
</person>
</persons>
i have tried all the examples at codehaus (which are all marshall-centric).
i have googled countless sites.
no where have i found a simple, complete example that does this most basic
of requirements.
if collections are created, they are either null or empty.
Lots of: No field "person" for implicit collection errors as well.
====== CODE =========
public class Persons {
private List<Person> persons = new ArrayList<Person>();
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
//this is a "command" class as one does not need a model class for
"persons" thus the List instead of Set (i was just following examples i
found online, but would rather it be a set as well).
//also, why examples have get() and add(), instead of POJO get(), set()?
---------
public class Person {
private String name;
private Set<Address> addresses = new HashSet<Address>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
this.addresses = addresses;
}
}
//are complete constructors required?
-------------
public class Address {
private String address1;
public String getAddress1() {
return address1;
}
public void setAddress1(String address1) {
this.address1 = address1;
}
}
--------------
public void personsFromXML(MultipartFile xml) {
XStream xstream = new XStream(new StaxDriver());
Persons persons = new Persons();
xstream.alias("persons", Persons.class);
try {
persons = (Persons)xstream.fromXML(xml.getInputStream());
}
catch(Exception e){
e.printStackTrace();
}
--------------
i have tried annotations in Persons.class as well as all variations of the
below (+ others):
//xstream.addImplicitCollection(Person.class, "person");
//xstream.alias("persons", Persons.class);
//xstream.alias("person", Person.class);
//xstream.alias("address", Address.class);
//xstream.addImplicitCollection(Person.class, "person");
//xstream.addImplicitCollection(Person.class, "person",
Persons.class);
//xstream.addImplicitCollection(Persons.class, "person",
Person.class);
how does xstream know that collection A is within Persons.class, but
collection B is in Person.class?
are the generic converters being used when xtream performs its
auto-deserialization magic?
are aliases and implicitCollections handled diff when marshall v.
unmarshall?
thx for the read,
-J