Hi John,

js wrote:

> 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).


Well, no. Not really. See, XStream is about Java object to XML *and back*, 
i.e. every example can also unmarshal.


> i have googled countless sites.
> no where have i found a simple, complete example that does this most basic
> of requirements.


Because it's not necessary? ... see below ;-) 

 
> 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).


You may also use directly an array (Person[]).


> //also, why examples have get() and add(), instead of POJO get(), set()?


Why do you think the objects have to be POJOs?

 
> ---------
> 
> 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?


Neither constructors nor setters are 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);


http://xstream.codehaus.org/faq.html#XML_unmarshalling_fails

Therefore adjust the marshalling. If XStream generates the required XML 
format, it can also read it. It is a very tedious and annoying task to try 
it the other way round (you already found this out), because you never know 
- especially as starter - why XStream throws an exception or silently skips 
some elements.

 
> how does xstream know that collection A is within Persons.class, but
> collection B is in Person.class?


Java reflection.


> are the generic converters being used when xtream performs its
> auto-deserialization magic?


Since XStream has obviously no built-in converters for Person or Address, it 
has to use the generic converters (based on reflection).


> are aliases and implicitCollections handled diff when marshall v.
> unmarshall?


No. That's the point. Adjust the XStream and write an object as XML and you 
will see immediately what has been influenced.

Hope this helps,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to