Java XStream
Today we will show you how to quickly get started with java XStream library. XStream library is used to parse xml files straight into the java classes. You need to annotate your object with appropriate annotations so that XStream knows how to convert xml to java object. Here is a simple example of a Book class.
Book example – simple case
Book.java
package com.itcuties.java.xstream.data;
import com.thoughtworks.xstream.annotations.XStreamAlias;
/**
* A class that reads the book tag. This is the most simple case. Only several fields
* of basic types.
*
* Of course feel free to add any other methods or un-annotated fields you wish.
*
* @author itcuties
*/
@XStreamAlias("book")
public class Book {
@XStreamAlias("title")
private String title;
@XStreamAlias("author")
private String author;
@XStreamAlias("pages-count")
private int pagesCount;//this can also be Integer or any other numeric type
private String fieldThatIsNotInTheXml = "field not in xml";
@Override
public String toString() {
return "Book [title=" + title + ", author=" + author + ", pagesCount="
+ pagesCount + ", fieldThatIsNotInTheXml="
+ fieldThatIsNotInTheXml + "]";
}
}
XStream uses @XStreamAlias annotation to map object and its attributes to xml elements. Here is a corresponding XML file.
book.xml
<book> <title>The Lord of The Rings</title> <author>J.R.R. Tolkien</author> <pages-count>1024</pages-count> </book>
To read a book from xml to java object you will need the following code.
// first create the xstream instance
XStream xStream = new XStream();
// then process the annotations on our class
xStream.processAnnotations(Book.class);
// load the xml and get the result
Object readObject = xStream.fromXML(new File("xmls/book.xml"));
// print the result
System.out.println("Object loaded by xstream: " + readObject);
If you run this code you will get the following output
Object loaded by xstream: Book [title=The Lord of The Rings, author=J.R.R. Tolkien, pagesCount=1024, fieldThatIsNotInTheXml=null]
Library example – using List
In this example you we will show you how to use List type and read it from XML to Java object.
Library.java
package com.itcuties.java.xstream.data;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
/**
* The purpose of this class is to present how to load several tags of the same type into a list.
*
* @author itcuties
*/
@XStreamAlias("library")
public class Library {
@XStreamAlias("name")
private String libraryName;
@XStreamImplicit(itemFieldName="book")//use this annotation and tell it what is the tag's name to add to the list
private List<Book> books;
@Override
public String toString() {
return "Library [libraryName=" + libraryName + ", books=" + books + "]";
}
}
You need to annotate attributes of type List with @XStreamImplicit(itemFieldName="book") annotation. Here is corresponding xml file.
<library> <name>The smallest library in the world.</name> <book> <title>The Lord of The Rings</title> <author>J.R.R. Tolkien</author> <pages-count>1024</pages-count> </book> <book> <title>Dune</title> <author>Frank Herbert</author> <pages-count>512</pages-count> </book> <book> <title>Quo Vadis</title> <author>Henryk Sienkiewicz</author> <pages-count>777</pages-count> </book> </library>
Run this code the same way as before (Read from XML) and you will get the following output:
Object loaded by xstream: Library [libraryName=The smallest library in the world., books=[Book [title=The Lord of The Rings, author=J.R.R. Tolkien, pagesCount=1024, fieldThatIsNotInTheXml=null], Book [title=Dune, author=Frank Herbert, pagesCount=512, fieldThatIsNotInTheXml=null], Book [title=Quo Vadis, author=Henryk Sienkiewicz, pagesCount=777, fieldThatIsNotInTheXml=null]]]
Shop example – using xml attributes
In this example we will show you how read java class attributes values from xml attributes. Here is an example.
Shop.java
package com.itcuties.java.xstream.data;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
/**
* This class shows how xml tag attributes can be loaded.
*
* @author itcuties
*/
@XStreamAlias("shop")
public class Shop {
@XStreamAsAttribute//just add this annotation to inform XStream that it will deal with attribute
@XStreamAlias("shop")
private String owner;
@XStreamAlias("open-hour")
@XStreamAsAttribute//it can be also placed after alias definition (just put it before the field)
private int openHour;
@XStreamAsAttribute
@XStreamAlias("close-hour")
private int closeHour;
@XStreamImplicit(itemFieldName="product")
private List<String> products;
@Override
public String toString() {
return "Shop [owner=" + owner + ", openHour=" + openHour
+ ", closeHour=" + closeHour + ", products=" + products + "]";
}
}
To tell XStream that class attribute value will be stored in a tag’s attribute a @XStreamAsAttribute annotation needs to be used. Here is an example XML file.
shop.xml
<shop owner="Mr. John Potato" open-hour="8" close-hour="20"> <product>Apples</product> <product>Bananas</product> <product>Bread</product> <product>Potatos</product> </shop>
Run this code the same way as before (Read from XML) and you will get the following output:
Object loaded by xstream: Shop [owner=Mr. John Potato, openHour=8, closeHour=20, products=[Apples, Bananas, Bread, Potatos]]
![]()
Download this sample code here.
![]()
This code is available on our GitHub repository as well.

Leave a Reply
Want to join the discussion?Feel free to contribute!