2010-07-05 23 views
1

有没有办法映射(使用xstream)List<Person><friends>List<Things><stuff>例如?通用集合和XStream

谢谢!

+0

请参阅我的答案[这里](HTTP:/ /stackoverflow.com/questions/3136234/xstream-root-as-a-collection-of-objects/3138114#3138114)。 – chedine 2010-07-05 17:59:09

+0

感谢,但这个例子是解组对象。我试图从对象到XML – 2010-07-05 18:12:27

回答

1

是。

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

    } 
} 

打印

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

如果你的意思是别的东西,请澄清问题。

+0

这将涉及到包装对象去。在你的情况下,'XStreamTest'。我在想,如果不这样做, – 2010-07-06 01:58:44

+0

它也不是没有测试代码中的一个良好的XML文档有可能,不是吗? – I82Much 2010-07-06 02:42:33

+0

你可以有朋友和东西列为单独的文件 – 2010-07-09 20:52:10

1

为什么不使用JAXB呢?如果你不喜欢的注解,你可以使用EclipseLink MOXyXML metata representation

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); 
    } 

} 

这将打印相同的XML作为XStream的例子:

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

看看我的博客文章比较JAXB及XStream:http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html – 2010-10-07 18:45:33