Conversion Between XML and object using xstream

Source: Internet
Author: User

Offical site said, but it does not make it clearly. Especial Parse XML with attributes. Here is my test according to docs from offical websites.

public class XstreamTest {        public static void main(String[] args) throws Exception {        //convertXml2();        convertObject2();    }        public static void convertXml1(){        RendezvousMessage1 message = new RendezvousMessage1(1, false, "part1","part2","part3");        XStream xStream = new XStream(new DomDriver());        xStream.processAnnotations(RendezvousMessage1.class);        System.out.println("message1 "+xStream.toXML(message));    }    public static String convertXml2(){        RendezvousMessage2 message = new RendezvousMessage2(1, false, "part1","part2","part3");        XStream xStream = new XStream(new DomDriver());        xStream.processAnnotations(RendezvousMessage2.class);        System.out.println("message2 "+xStream.toXML(message));        String xmlString = xStream.toXML(message);        return xmlString;    }        public static void convertObject2(){                XStream xStream = new XStream(new DomDriver());        xStream.processAnnotations(RendezvousMessage2.class);        System.out.println("convertObject2 "+xStream.fromXML(convertXml2()));    }    }@XStreamAlias("message")class RendezvousMessage1 {    @XStreamAlias("type")       @XStreamAsAttribute    private int messageType;    @XStreamImplicit(itemFieldName="part")    private List<String> content;           @XStreamAsAttribute    @XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"yes", "no"})    private boolean important;    public RendezvousMessage1(int messageType, boolean important, String... content) {        this.messageType = messageType;        this.important = important;        this.content = Arrays.asList(content);    }}@XStreamAlias("message")class RendezvousMessage2 {       @XStreamAsAttribute    private int type;    @XStreamImplicit(itemFieldName="part")    private List<String> content;           @XStreamAsAttribute    @XStreamConverter(value=BooleanConverter.class, booleans={false}, strings={"yes", "no"})    private boolean important;    public RendezvousMessage2(int messageType, boolean important, String... content) {        this.type = messageType;        this.important = important;        this.content = Arrays.asList(content);    }    @Override    public String toString() {        return "type "+type+" content "+content;    }}

Through my test, it work well.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.