code example for using XStream annotations to convert Java objects to and from XML

Source: Internet
Author: User

This article records some code examples that use the annotations feature of the XStream API to convert Java objects to and from XML strings.
Many of us have worked with XML files, and there are many very mature third-party open source software. such as:Jdom,dom4j and so on. Although they are very powerful, they are still a bit less accustomed to use. For a fixed-format XML document, its structure does not change or change very little, then it is converted to our familiar Java object to operate, it will make the work easier, and XStream just can meet this point.
the version of XStream used in this article is:1.4.7

<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>

Or take the previous book XML as an example, first on the code.

Import Com.thoughtworks.xstream.annotations.XStreamAlias;
Import Com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias ("book")
public class Book {

An alias annotation, which is an element name in an XML document, and the Java attribute name does not necessarily match the alias
@XStreamAlias ("name")
private String name;

@XStreamAlias ("author")
Private String author;

attribute annotations, this price is the property of the book, shown in XML as: <book price= "108" >
@XStreamAsAttribute ()
@XStreamAlias ("Price")
Private String Price;

Omit get and Set methods
}


Import java.util.List;

Import Com.thoughtworks.xstream.annotations.XStreamAlias;
Import com.thoughtworks.xstream.annotations.XStreamImplicit;

@XStreamAlias ("Books")
public class Books {

The implicit collection, with this annotation, removes the outermost <list></list> of the book Collection
@XStreamImplicit
Private list<book> List;

Omit get and Set methods
}


Import java.util.List;

Import Com.thoughtworks.xstream.XStream;
Import Com.thoughtworks.xstream.io.xml.DomDriver;

public class Xstreamhandle {

private static final String xmlstring = "<books><book price=\" 108\ "><name>java programming thought </name>< Author>bruce eckel</author></book><book price=\ "52\" ><name>effective Java</name> <author>joshua bloch</author></book><book price=\ "118\" ><name>java 7 Getting Started classic </name ><author>ivor horton</author></book></books> ";

public static String toXml (Object obj) {
XStream XStream = new XStream (new Domdriver ("UTF8"));
Xstream.processannotations (Obj.getclass ()); Identify annotations in the Obj class
/*
Output XML in a compressed manner
StringWriter SW = new StringWriter ();
Xstream.marshal (obj, new Compactwriter (SW));
return sw.tostring ();
*/
Output XML in a formatted manner
return Xstream.toxml (obj);
}

public static <T> T Tobean (String xmlstr, class<t> CLS) {
XStream XStream = new XStream (new Domdriver ());
Xstream.processannotations (CLS);
@SuppressWarnings ("Unchecked")
T t = (t) xstream.fromxml (XMLSTR);
return t;
}

public static void Main (string[] args) {
Books Books = Tobean (xmlstring, Books.class);
list<book> list = Books.getlist ();
for (book book:list) {
System.out.println ("name=" + book.getname () + "\tauthor=" + book.getauthor ()
+ "\tprice=" + Book.getprice ());
}
System.out.println (toXml (books));
}
}


In addition to the annotations in the example above, XStream also has several annotations that are often used.
@XstreamOmitField Ignore Fields
This is equivalent to setting some fields as temporary properties that no longer work in the conversion.
@XStreamConverter(Xxx.class) Converter
    xxx. class is a converter that implements the com.thoughtworks.xstream.converters.converter interface, converting certain types of values, such as the true or false , and if you do not add a converter, the default generated value is true or false. xstream comes with booleanconverter converter, you can convert the default value to the desired text value, if the xstream does not need a translator to implement converter interface to customize the converter.
     based on elephant experience, in order to avoid trouble for themselves, such as avoiding the use of converters, it is best to xml element or attribute corresponding string type, but the list is also defined as list type. As long as it is not a special work, in general, the sample part can meet most of the requirements.
     This is the original pineapple elephant, if you want to reprint please indicate the source. Http://www.blogjava.net/bolo

Code example for using XStream annotations to convert Java objects to and from XML

Related Article

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.