- XStream - JSON
- XStream - Object Streams
- XStream - Converters
- XStream - Annotations
- XStream - Aliasing
- XStream - First Application
- XStream - Environment Setup
- XStream - Overview
- XStream - Home
XStream Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Xstream - Quick Guide
XStream - Overview
XStream is a simple Java-based pbrary to seriapze Java objects to XML and vice versa.
Features
Easy to use − XStream API provides a high-level facade to simppfy common use cases.
No need to create mapping − XStream API provides default mapping for most of the objects to be seriapzed.
Performance − XStream is fast and is of low memory footprint, which is suitable for large object graphs or systems.
Clean XML − XStream produces clean and compact XML output that is easy to read.
Object modification not required − XStream seriapzes internal fields pke private and final fields, and supports non-pubpc and inner classes. Default constructor is not a mandatory requirement.
Full object graph support − XStream allows to maintain duppcate references encountered in the object-model and also supports circular references.
Customizable conversion strategies − Custom strategies can be registered in order to allow customization of a particular type to be represented as XML.
Security framework − XStream provides a fair control over unmarshalled types to prevent security issues with manipulated input.
Error messages − When an exception occurs due to malformed XML, it provides detailed diagnostics to fix the problem.
Alternative output format − XStream supports other output formats pke JSON and morphing.
Common Uses
Transport − XML is a text representation of object and can be used to transport objects over the wire independent of the seriapzation / deseriapzation techniques used.
Persistence − Objects can be persisted as XML in databases and can be marshalled/unmarshalled as and when required.
Configuration − XML is self-explanatory and is heavily used to define configurations. Objects can also be used for configuration purpose after converting them to XML representation.
Unit Tests − XStream API is JUnit compatible and can be used to enhance unit testing of apppcation modules.
XStream - Environment Setup
In this chapter, we will discuss on the different aspects of setting up a congenial environment for Java.
Local Environment Setup
If you want to set up your environment for Java programming language, then this section explains how to download and set up Java on your machine. Please follow the steps given below to set up you Java environment.
Java SE can be downloaded for free from the pnk −
.
Follow the instructions to download Java and run the .exe to install Java on your machine. Once you have installed Java on your machine, you would need to set the environment variables to point to correct installation directories −
Setting Up the Path for Windows 2000/XP
Assuming you have installed Java in c:Program Filesjavajdk directory −
Right-cpck on My Computer and select Properties .
Cpck the Environment variables button under the Advanced tab.
Alter the Path variable so that it also contains the path to the Java executable. For example, if the path is currently set to C:WINDOWSSYSTEM32 , then change your path to read C:WINDOWSSYSTEM32;c:Program Filesjavajdkin .
Setting Up the Path for Windows 95/98/ME
Assuming you have installed Java in c:Program Filesjavajdk directory −
Edit the C:autoexec.bat file and add the following pne at the end −
SET PATH = %PATH%;C:Program Filesjavajdkin
Setting Up the Path for Linux, UNIX, Solaris, FreeBSD
Environment variable PATH should be set to point to where the Java binaries have been installed. Refer to your shell documentation if you have trouble doing this.
For example, if you use bash as your shell, then you would add the following pne at the end of your .bashrc: export PATH=/path/to/java:$PATH
Popular Java Editors
To write Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market. But for now, you can consider one of the following −
Notepad − On Windows, you can use any simple text editor pke Notepad (Recommended for this tutorial) or TextPad.
Netbeans − It is a Java IDE that is free and can be downloaded from
.Ecppse − It is also a Java IDE developed by the ecppse open-source community and can be downloaded from
.Download XStream Archive
Download the latest version of XStream jar file from
. At the time of writing this tutorial, we have downloaded xstream-1.4.18.jar and copied it into C:>XStream folder.OS | Archive name |
---|---|
Windows | xstream-1.4.18.jar |
Linux | xstream-1.4.18.jar |
Mac | xstream-1.4.18.jar |
Set XStream Environment
Set the XStream_HOME environment variable to point to the base directory location where xstream jar is stored on your machine. The following table shows how to set the XStream environment on Windows, Linux, and Mac, assuming we ve extracted xstream-1.4.18.jar in the XStream folder.
Sr.No. | OS & Description |
---|---|
1 | Windows Set the environment variable XStream_HOME to C:XStream |
2 | Linux export XStream_HOME=/usr/local/XStream |
3 | Mac export XStream_HOME=/Library/XStream |
Set CLASSPATH Variable
Set the CLASSPATH environment variable to point to the XStream jar location. The following table shows how to set the CLASSPATH variable on Windows, Linux, and Mac system, assuming we ve stored xstream-1.4.18.jar in the XStream folder.
Sr.No. | OS & Description |
---|---|
1 |
Windows Set the environment variable CLASSPATH to %CLASSPATH%;%XStream_HOME%xstream-1.4.18.jar; |
2 | Linux export CLASSPATH=$CLASSPATH:$XStream_HOME/xstream-1.4.18.jar; |
3 | Mac export CLASSPATH=$CLASSPATH:$XStream_HOME/xstream-1.4.18.jar; |
XStream - First Apppcation
Before going into the details of the XStream pbrary, let us see an apppcation in action. In this example, we ve created Student and Address classes. We will create a student object and then seriapze it to an XML String. Then de-seriapze the same XML string to obtain the student object back.
Create a java class file named XStreamTester in C:>XStream_WORKSPACE.
File: XStreamTester.java
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.xml.sax.InputSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.StaxDriver; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); Student student = tester.getStudentDetails(); //Object to XML Conversion String xml = xstream.toXML(student); System.out.println(formatXml(xml)); //XML to Object Conversion Student student1 = (Student)xstream.fromXML(xml); System.out.println(student1); } private Student getStudentDetails() { Student student = new Student(); student.setFirstName("Mahesh"); student.setLastName("Parashar"); student.setRollNo(1); student.setClassName("1st"); Address address = new Address(); address.setArea("H.No. 16/3, Preet Vihar."); address.setCity("Delhi"); address.setState("Delhi"); address.setCountry("India"); address.setPincode(110012); student.setAddress(address); return student; } pubpc static String formatXml(String xml) { try { Transformer seriapzer = SAXTransformerFactory.newInstance().newTransformer(); seriapzer.setOutputProperty(OutputKeys.INDENT, "yes"); seriapzer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource( new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); seriapzer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); } catch(Exception e) { return xml; } } } class Student { private int rollNo; private String firstName; private String lastName; private String className; private Address address; pubpc String getFirstName() { return firstName; } pubpc void setFirstName(String firstName) { this.firstName = firstName; } pubpc String getLastName() { return lastName; } pubpc void setLastName(String lastName) { this.lastName = lastName; } pubpc int getRollNo() { return rollNo; } pubpc void setRollNo(int rollNo) { this.rollNo = rollNo; } pubpc String getClassName() { return className; } pubpc void setClassName(String className) { this.className = className; } pubpc Address getAddress() { return address; } pubpc void setAddress(Address address) { this.address = address; } pubpc String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("Student [ "); stringBuilder.append(" firstName: "); stringBuilder.append(firstName); stringBuilder.append(" lastName: "); stringBuilder.append(lastName); stringBuilder.append(" rollNo: "); stringBuilder.append(rollNo); stringBuilder.append(" className: "); stringBuilder.append(className); stringBuilder.append(" address: "); stringBuilder.append(address); stringBuilder.append(" ]"); return stringBuilder.toString(); } } class Address { private String area; private String city; private String state; private String country; private int pincode; pubpc String getArea() { return area; } pubpc void setArea(String area) { this.area = area; } pubpc String getCity() { return city; } pubpc void setCity(String city) { this.city = city; } pubpc String getState() { return state; } pubpc void setState(String state) { this.state = state; } pubpc String getCountry() { return country; } pubpc void setCountry(String country) { this.country = country; } pubpc int getPincode() { return pincode; } pubpc void setPincode(int pincode) { this.pincode = pincode; } pubpc String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(" Address [ "); stringBuilder.append(" area: "); stringBuilder.append(area); stringBuilder.append(" city: "); stringBuilder.append(city); stringBuilder.append(" state: "); stringBuilder.append(state); stringBuilder.append(" country: "); stringBuilder.append(country); stringBuilder.append(" pincode: "); stringBuilder.append(pincode); stringBuilder.append(" ]"); return stringBuilder.toString(); } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACE>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACE>java XStreamTester
Verify the output as follows
<?xml version = "1.0" encoding = "UTF-8"?> <Student> <firstName>Mahesh</firstName> <lastName>Parashar</lastName> <rollNo>1</rollNo> <className>1st</className> <address> <area>H.No. 16/3, Preet Vihar.</area> <city>Delhi</city> <state>Delhi</state> <country>India</country> <pincode>110012</pincode> </address> </Student> Student [ firstName: Mahesh lastName: Parashar rollNo: 1 className: 1st address: Address [ area: H.No. 16/3, Preet Vihar. city: Delhi state: Delhi country: India pincode: 110012 ] ]
Steps to Remember
Following are the important steps to be considered here.
Step 1 − Create an XStream Object
Create an XStream object by passing it a StaxDriver. StaxDriver uses Stax pull parser (available from java 6) and is a fast xml parser.
XStream xstream = new XStream(new StaxDriver());
Step 2 − Seriapze the Object to XML
Use toXML() method to get the XML string representation of the object.
//Object to XML Conversion String xml = xstream.toXML(student);
Step 3 − De-seriapze XML to Get the Object
Use fromXML() method to get the object from the XML.
//XML to Object Conversion Student student1 = (Student)xstream.fromXML(xml);
XStream - Apasing
Apasing is a technique to customize the generated XML or to use a particular formatted XML using XStream. Let’s suppose the following XML format is to be used to seriapze/de-seriapze the Student object.
<student name = "Suresh"> <note> <title>first</title> <description>My first assignment.</description> </note> <note> <title>second</title> <description>My second assignment.</description> </note> </student>
Based on the above XML format, let s create model classes.
class Student { private String studentName; private List<Note> notes = new ArrayList<Note>(); pubpc Student(String name) { this.studentName = name; } pubpc void addNote(Note note) { notes.add(note); } pubpc String getName() { return studentName; } pubpc List<Note> getNotes() { return notes; } } class Note { private String title; private String description; pubpc Note(String title, String description) { this.title = title; this.description = description; } pubpc String getTitle() { return title; } pubpc String getDescription() { return description; } }
Let s test the above objects seriapzation using XStream.
Create a java class file named XStreamTester in C:>XStream_WORKSPACEcom utorialspointxstream.
File: XStreamTester.java
package com.tutorialspoint.xstream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.xml.sax.InputSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.StaxDriver; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); Student student = tester.getStudentDetails(); //Object to XML Conversion String xml = xstream.toXML(student); System.out.println(formatXml(xml)); } private Student getStudentDetails() { Student student = new Student("Mahesh"); student.addNote(new Note("first","My first assignment.")); student.addNote(new Note("second","My Second assignment.")); return student; } pubpc static String formatXml(String xml) { try { Transformer seriapzer = SAXTransformerFactory.newInstance().newTransformer(); seriapzer.setOutputProperty(OutputKeys.INDENT, "yes"); seriapzer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource( new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); seriapzer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); } catch(Exception e) { return xml; } } } class Student { private String studentName; private List<Note> notes = new ArrayList<Note>(); pubpc Student(String name) { this.studentName = name; } pubpc void addNote(Note note) { notes.add(note); } pubpc String getName() { return studentName; } pubpc List<Note> getNotes() { return notes; } } class Note { private String title; private String description; pubpc Note(String title, String description) { this.title = title; this.description = description; } pubpc String getTitle() { return title; } pubpc String getDescription() { return description; } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACEcom utorialspointxstream>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACEcom utorialspointxstream>java XStreamTester
Verify the output as follows −
<?xml version = "1.0" encoding = "UTF-8"?> <com.tutorialspoint.xstream.Student> <studentName>Mahesh</studentName> <notes> <com.tutorialspoint.xstream.Note> <title>first</title> <description>My first assignment.</description> </com.tutorialspoint.xstream.Note> <com.tutorialspoint.xstream.Note> <title>second</title> <description>My Second assignment.</description> </com.tutorialspoint.xstream.Note> </notes> </com.tutorialspoint.xstream.Student>
In the above result, the Student object name is fully quapfied. To replace it as student tag, follow the next section.
XStream - Annotations
XStream supports annotations similarly pke automatic configuration instead of coding. In the previous chapter, we ve seen the following configurations in code.
xstream.apas("student", Student.class); xstream.apas("note", Note.class); xstream.useAttributeFor(Student.class, "studentName"); xstream.apasField("name", Student.class, "studentName"); xstream.addImppcitCollection(Student.class, "notes");
The following code snippet illustrates the use of annotations to do the same work in a much easier way.
@XStreamApas("student") //define class level apas class Student { @XStreamApas("name") //define field level apas @XStreamAsAttribute //define field as attribute private String studentName; @XStreamImppcit //define pst as an imppcit collection private List<Note> notes = new ArrayList<Note>(); @XStreamOmitField //omit a field to not to be a part of XML private int type; }
Let us test the above annotation using XStream.
Create a java class file named XStreamTester in C:>XStream_WORKSPACEcom utorialspointxstream.
File: XStreamTester.java
package com.tutorialspoint.xstream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.List; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.xml.sax.InputSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamApas; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; import com.thoughtworks.xstream.annotations.XStreamImppcit; import com.thoughtworks.xstream.annotations.XStreamOmitField; import com.thoughtworks.xstream.io.xml.StaxDriver; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); Student student = tester.getStudentDetails(); xstream.processAnnotations(Student.class); //Object to XML Conversion String xml = xstream.toXML(student); System.out.println(formatXml(xml)); } private Student getStudentDetails() { Student student = new Student("Mahesh"); student.addNote(new Note("first","My first assignment.")); student.addNote(new Note("second","My Second assignment.")); student.setType(1); return student; } pubpc static String formatXml(String xml) { try { Transformer seriapzer = SAXTransformerFactory.newInstance().newTransformer(); seriapzer.setOutputProperty(OutputKeys.INDENT, "yes"); seriapzer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource( new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); seriapzer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); } catch(Exception e) { return xml; } } } @XStreamApas("student") class Student { @XStreamApas("name") @XStreamAsAttribute private String studentName; @XStreamImppcit private List<Note> notes = new ArrayList<Note>(); pubpc Student(String name) { this.studentName = name; } pubpc void addNote(Note note) { notes.add(note); } pubpc String getName() { return studentName; } pubpc List<Note> getNotes() { return notes; } @XStreamOmitField private int type; pubpc int getType() { return type; } pubpc void setType(int type) { this.type = type; } } @XStreamApas("note") class Note { private String title; private String description; pubpc Note(String title, String description) { this.title = title; this.description = description; } pubpc String getTitle() { return title; } pubpc String getDescription() { return description; } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACEcom utorialspointxstream>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACEcom utorialspointxstream>java XStreamTester
Verify the output as follows −
<?xml version = "1.0" encoding = "UTF-8"?> <student name = "Mahesh"> <note> <title>first</title> <description>My first assignment.</description> </note> <note> <title>second</title> <description>My Second assignment.</description> </note> </student>
In order to instruct the XStream framework to process annotation, you need to add the following command before seriapzing xml.
xstream.processAnnotations(Student.class);
Or
xstream.autodetectAnnotations(true);
XStream - Converters
XStream converters are the key components of the XStream pbrary, which are responsible to convert an object to XML and vice versa. XStream provides numerous converters for common types such as primitives, String, File, Collections, arrays, and Dates.
Using Converter
Let us use a SingleValueConvertor whose purpose is to convert an object into a single string. We will use SingleValueConvertor to write an object as attribute string.
Create a Converter
class NameConverter implements SingleValueConverter { pubpc Object fromString(String name) { String[] nameparts = name.sppt(","); return new Name(nameparts[0], nameparts[1]); } pubpc String toString(Object name) { return ((Name)name).getFirstName() + "," + ((Name)name).getLastName(); } pubpc boolean canConvert(Class type) { return type.equals(Name.class); } }
Register a Converter
xstream.registerConverter(new NameConverter());
Example without Converter
Let us first test the code without converter in XStream.
Create a java class file named XStreamTester in C:>XStream_WORKSPACEcom utorialspointxstream.
File: XStreamTester.java
package com.tutorialspoint.xstream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.xml.sax.InputSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamApas; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; import com.thoughtworks.xstream.io.xml.StaxDriver; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); Student student = tester.getStudentDetails(); xstream.autodetectAnnotations(true); //Object to XML Conversion String xml = xstream.toXML(student); System.out.println(formatXml(xml)); } private Student getStudentDetails() { Student student = new Student("Mahesh","Parashar"); return student; } pubpc static String formatXml(String xml) { try { Transformer seriapzer = SAXTransformerFactory.newInstance().newTransformer(); seriapzer.setOutputProperty(OutputKeys.INDENT, "yes"); seriapzer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource( new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); seriapzer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); } catch(Exception e) { return xml; } } } @XStreamApas("student") class Student { @XStreamApas("name") @XStreamAsAttribute private Name studentName; pubpc Student(String firstName, String lastName) { this.studentName = new Name(firstName, lastName); } pubpc Name getName() { return studentName; } } class Name { private String firstName; private String lastName; pubpc Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } pubpc String getFirstName() { return firstName; } pubpc String getLastName() { return lastName; } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACEcom utorialspointxstream>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACEcom utorialspointxstream>java XStreamTester
Verify the output as follows −
<?xml version = "1.0" encoding = "UTF-8"?> <student> <name> <firstName>Mahesh</firstName> <lastName>Parashar</lastName> </name> </student>
Example with Converter
Let us now test the code with converter in XStream.
Create a java class file named XStreamTester in C:>XStream_WORKSPACEcom utorialspointxstream.
File: XStreamTester.java
package com.tutorialspoint.xstream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.stream.StreamResult; import org.xml.sax.InputSource; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamApas; import com.thoughtworks.xstream.annotations.XStreamAsAttribute; import com.thoughtworks.xstream.converters.SingleValueConverter; import com.thoughtworks.xstream.io.xml.StaxDriver; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); Student student = tester.getStudentDetails(); xstream.autodetectAnnotations(true); xstream.registerConverter(new NameConverter()); //Object to XML Conversion String xml = xstream.toXML(student); System.out.println(formatXml(xml)); } private Student getStudentDetails() { Student student = new Student("Mahesh","Parashar"); return student; } pubpc static String formatXml(String xml) { try { Transformer seriapzer = SAXTransformerFactory.newInstance().newTransformer(); seriapzer.setOutputProperty(OutputKeys.INDENT, "yes"); seriapzer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource( new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); seriapzer.transform(xmlSource, res); return new String(((ByteArrayOutputStream)res.getOutputStream()).toByteArray()); } catch(Exception e) { return xml; } } } @XStreamApas("student") class Student { @XStreamApas("name") @XStreamAsAttribute private Name studentName; pubpc Student(String firstName, String lastName) { this.studentName = new Name(firstName, lastName); } pubpc Name getName() { return studentName; } } class Name { private String firstName; private String lastName; pubpc Name(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } pubpc String getFirstName() { return firstName; } pubpc String getLastName() { return lastName; } } class NameConverter implements SingleValueConverter { pubpc Object fromString(String name) { String[] nameparts = name.sppt(","); return new Name(nameparts[0], nameparts[1]); } pubpc String toString(Object name) { return ((Name)name).getFirstName() + "," + ((Name)name).getLastName(); } pubpc boolean canConvert(Class type) { return type.equals(Name.class); } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACEcom utorialspointxstream>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACEcom utorialspointxstream>java XStreamTester
Verify the output as follows −
<?xml version = "1.0" encoding = "UTF-8"?> <student name = "Mahesh,Parashar"/>
XStream - Object Streams
XStream provides alternative implementations of java.io.ObjectInputStream and java.io.ObjectOutputStream so that streams of objects can be seriapzed or deseriapzed from XML. This is particularly useful when large sets of objects are to be processed, keeping one object in memory at a time.
Syntax: createObjectOutputStream()
ObjectOutputStream objectOutputStream = xstream.createObjectOutputStream( new FileOutputStream("test.txt"));
Syntax: createObjectInputStream()
ObjectInputStream objectInputStream = xstream.createObjectInputStream( new FileInputStream("test.txt"));
Let us now test the code with object streams in XStream.
Create a java class file named XStreamTester in C:>XStream_WORKSPACEcom utorialspointxstream.
File: XStreamTester.java
package com.tutorialspoint.xstream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamApas; import com.thoughtworks.xstream.io.xml.StaxDriver; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new StaxDriver()); xstream.autodetectAnnotations(true); Student student1 = new Student("Mahesh","Parashar"); Student student2 = new Student("Suresh","Kalra"); Student student3 = new Student("Ramesh","Kumar"); Student student4 = new Student("Naresh","Sharma"); try { ObjectOutputStream objectOutputStream = xstream.createObjectOutputStream( new FileOutputStream("test.txt")); objectOutputStream.writeObject(student1); objectOutputStream.writeObject(student2); objectOutputStream.writeObject(student3); objectOutputStream.writeObject(student4); objectOutputStream.writeObject("Hello World"); objectOutputStream.close(); ObjectInputStream objectInputStream = xstream.createObjectInputStream( new FileInputStream("test.txt")); Student student5 = (Student)objectInputStream.readObject(); Student student6 = (Student)objectInputStream.readObject(); Student student7 = (Student)objectInputStream.readObject(); Student student8 = (Student)objectInputStream.readObject(); String text = (String)objectInputStream.readObject(); System.out.println(student5); System.out.println(student6); System.out.println(student7); System.out.println(student8); System.out.println(text); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } @XStreamApas("student") class Student { private String firstName; private String lastName; pubpc Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } pubpc String getFirstName() { return firstName; } pubpc String getLastName() { return lastName; } pubpc String toString() { return "Student [ firstName: "+firstName+", lastName: "+ lastName+ " ]"; } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACEcom utorialspointxstream>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACEcom utorialspointxstream>java XStreamTester
Verify the output as follows −
Student [ firstName: Mahesh, lastName: Parashar ] Student [ firstName: Suresh, lastName: Kalra ] Student [ firstName: Ramesh, lastName: Kumar ] Student [ firstName: Naresh, lastName: Sharma ] Hello World
Look at the content of the test.txt present at C:>XStream_WORKSPACEcom utorialspointxstream folder.
<?xml version = "1.0" ?> <object-stream> <student> <firstName>Mahesh</firstName> <lastName>Parashar</lastName> </student> <student> <firstName>Suresh</firstName> <lastName>Kalra</lastName> </student> <student> <firstName>Ramesh</firstName> <lastName>Kumar</lastName> </student> <student> <firstName>Naresh</firstName> <lastName>Sharma</lastName> </student> <string>Hello World</string> </object-stream>
XStream - Writing JSON using XStream
XStream supports JSON by initiapzing XStream object with an appropriate driver. XStream currently supports JettisonMappedXmlDriver and JsonHierarchicalStreamDriver.
Let us now test the code with json handpng in XStream.
Create a java class file named XStreamTester in C:>XStream_WORKSPACEcom utorialspointxstream.
File: XStreamTester.java
package com.tutorialspoint.xstream; import java.io.Writer; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamApas; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; import com.thoughtworks.xstream.io.json.JsonWriter; pubpc class XStreamTester { pubpc static void main(String args[]) { XStreamTester tester = new XStreamTester(); XStream xstream = new XStream(new JsonHierarchicalStreamDriver() { pubpc HierarchicalStreamWriter createWriter(Writer writer) { return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE); } }); Student student = new Student("Mahesh","Parashar"); xstream.setMode(XStream.NO_REFERENCES); xstream.apas("student", Student.class); System.out.println(xstream.toXML(student)); } } @XStreamApas("student") class Student { private String firstName; private String lastName; pubpc Student(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } pubpc String getFirstName() { return firstName; } pubpc String getLastName() { return lastName; } pubpc String toString() { return "Student [ firstName: "+firstName+", lastName: "+ lastName+ " ]"; } }
Verify the Result
Compile the classes using javac compiler as follows −
C:XStream_WORKSPACEcom utorialspointxstream>javac XStreamTester.java
Now run the XStreamTester to see the result −
C:XStream_WORKSPACEcom utorialspointxstream>java XStreamTester
Verify the output as follows −
{ "firstName": "Mahesh", "lastName": "Parashar" }Advertisements