通用集合和 XStream

发布于 2024-09-08 13:56:48 字数 183 浏览 8 评论 0原文

有没有办法将 List 映射(使用 xstream)到 并将 List 映射到 例如

谢谢!

Is there a way to map (using xstream) a List<Person> to <friends> and List<Things> to <stuff> for example?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎樣才叫好 2024-09-15 13:56:48

是的。

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>

如果您有其他意思,请澄清问题。

Yes.

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>

If you mean something else, please clarify the question.

多孤肩上扛 2024-09-15 13:56:48

为什么不使用 JAXB 来代替呢?如果您不喜欢这些注释,可以使用 EclipseLink MOXyXML 元数据表示

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>

Why not use JAXB instead? If you don't like the annotations you can use EclipseLink MOXy's XML 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);
    }

}

This prints the same XML as the XStream example:

<root>
    <friends>
        <friend>
            <name>Fred</name>
        </friend>
    </friends>
    <stuff>
        <stuff>
            <description>Xbox 360</description>
        </stuff>
    </stuff>
</root>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文