1

有没有办法将(使用xstream)映射到List<Person>例如?<friends>List<Things><stuff>

谢谢!

4

2 回答 2

1

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>
于 2010-07-09T18:52:55.577 回答
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>

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

于 2010-07-05T23:38:33.493 回答