【问题标题】:Generic Collections and XStream泛型集合和 XStream
【发布时间】:2010-07-05 17:54:10
【问题描述】:

例如,有没有办法将List<Person> 映射到<friends>List<Things><stuff>

谢谢!

【问题讨论】:

  • 请参考我的回答here
  • 谢谢,但该示例用于解组对象。我正在尝试从对象转到 xml

标签: java xml mapping xstream oxm


【解决方案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>

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

【讨论】:

  • 这将涉及 Wrapping 对象。在你的情况下,XStreamTest。我想知道如果不这样做是否有可能
  • 如果没有测试标签,它就不会是一个格式良好的 XML 文档,不是吗?
  • 您可以将朋友和物品列表作为单独的文档
【解决方案2】:

为什么不改用 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);
    }

}

这将打印与 XStream 示例相同的 XML:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-12-24
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多