2010-07-05 8 views
1

Existe-t-il un moyen de mapper (en utilisant xstream) un List<Person> à <friends> et List<Things> à <stuff> par exemple?Collections génériques et XStream

Merci!

+0

S'il vous plaît se référer ma réponse [ici] (http://stackoverflow.com/questions/3136234/xstream-root-as-a-collection-of-objects/3138114#3138114). – chedine

+0

merci, mais cet exemple est pour unmarshalling des objets. J'essaye de passer des objets au xml –

Répondre

1

Oui.

import com.thoughtworks.xstream.XStream; 
import com.thoughtworks.xstream.io.xml.DomDriver; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

/** 
* 
* @author nicholasdunn 
*/ 
public class XStreamTest { 
    private List<Person> friends = new ArrayList<Person>(); 
    private List<Thing> stuff = new ArrayList<Thing>(); 

    public XStreamTest(List<Person> people, List<Thing> stuff) { 
     this.friends.addAll(people); 
     this.stuff.addAll(stuff); 
    } 

    private static class Person { 
     private String name; 

     public Person(String name) { 
      this.name = name; 
     } 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 


    } 
    private static class Thing { 
     private String description; 

     public Thing(String description) { 
      this.description = description; 
     } 

     public String getDescription() { 
      return description; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 

    }; 


    public static void main(String[] args) { 
     XStream xstream = new XStream(new DomDriver()); 
     xstream.alias("test", XStreamTest.class); 
     xstream.alias("person", Person.class); 
     xstream.alias("thing", Thing.class); 

     XStreamTest test = new XStreamTest(Arrays.asList(new Person("Fred")), Arrays.asList(new Thing("Xbox 360"))); 
     System.out.println(xstream.toXML(test)); 

    } 
} 

Prints

<test> 
    <friends> 
    <person> 
     <name>Fred</name> 
    </person> 
    </friends> 
    <stuff> 
    <thing> 
     <description>Xbox 360</description> 
    </thing> 
    </stuff> 
</test> 

Si vous voulez dire autre chose, s'il vous plaît clarifier la question.

+0

Cela impliquerait un objet Wrapping. Dans votre cas, 'XStreamTest'. Je me demandais si c'était possible sans faire cela –

+0

ce ne serait pas un document XML bien formé sans la balise de test, n'est-ce pas? – I82Much

+0

vous pourriez avoir les amis et les listes de choses en tant que documents séparés –

1

Pourquoi ne pas utiliser JAXB à la place? Si vous n'aimez pas les annotations que vous pouvez utiliser XML metata representation de EclipseLink MOXy:

import java.util.Arrays; 
import java.util.List; 

import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
import javax.xml.bind.annotation.XmlElementWrapper; 
import javax.xml.bind.annotation.XmlRootElement; 

public class JAXBTest { 

    @XmlRootElement 
    public static class Root { 
     private List<Person> friend; 
     private List<Thing> stuff; 

     @XmlElementWrapper(name="friends") 
     public List<Person> getFriend() { 
      return friend; 
     } 

     public void setFriend(List<Person> friend) { 
      this.friend = friend; 
     } 

     @XmlElementWrapper(name="stuff") 
     public List<Thing> getStuff() { 
      return stuff; 
     } 

     public void setStuff(List<Thing> stuff) { 
      this.stuff = stuff; 
     } 
    } 

    public static class Person { 
     private String name; 

     public String getName() { 
      return name; 
     } 

     public void setName(String name) { 
      this.name = name; 
     } 
    } 

    public static class Thing { 
     private String description; 

     public String getDescription() { 
      return description; 
     } 

     public void setDescription(String description) { 
      this.description = description; 
     } 
    } 

    public static void main(String[] args) throws JAXBException { 
     JAXBContext jaxbContext = JAXBContext.newInstance(Root.class); 
     Root root = new Root(); 

     Person fred = new Person(); 
     fred.setName("Fred"); 
     root.setFriend(Arrays.asList(fred)); 

     Thing xbox = new Thing(); 
     xbox.setDescription("Xbox 360"); 
     root.setStuff(Arrays.asList(xbox)); 

     Marshaller marshaller = jaxbContext.createMarshaller(); 
     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     marshaller.marshal(root, System.out); 
    } 

} 

Cette imprime le même XML que l'exemple XStream:

<root> 
    <friends> 
     <friend> 
      <name>Fred</name> 
     </friend> 
    </friends> 
    <stuff> 
     <stuff> 
      <description>Xbox 360</description> 
     </stuff> 
    </stuff> 
</root> 
+1

Vérifiez mon billet de blog comparant JAXB et XStream: http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html –

Questions connexes