XStream - Object to xml
XStream - Object to xml
This commit is contained in:
		
							parent
							
								
									eabccf2b8f
								
							
						
					
					
						commit
						a6057698e1
					
				
							
								
								
									
										15
									
								
								xstream-introduction/.classpath
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								xstream-introduction/.classpath
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <classpath> | ||||
| 	<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/> | ||||
| 	<classpathentry excluding="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/> | ||||
| 	<classpathentry including="**/*.java" kind="src" path="src/main/java"/> | ||||
| 	<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/> | ||||
| 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||||
| 	<classpathentry kind="var" path="M2_REPO/com/thoughtworks/xstream/xstream/1.4.5/xstream-1.4.5.jar" sourcepath="/M2_REPO/com/thoughtworks/xstream/xstream/1.4.5/xstream-1.4.5-sources.jar"/> | ||||
| 	<classpathentry kind="var" path="M2_REPO/xmlpull/xmlpull/1.1.3.1/xmlpull-1.1.3.1.jar"/> | ||||
| 	<classpathentry kind="var" path="M2_REPO/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c.jar" sourcepath="M2_REPO/xpp3/xpp3_min/1.1.4c/xpp3_min-1.1.4c-sources.jar"/> | ||||
| 	<classpathentry kind="var" path="M2_REPO/junit/junit/4.12/junit-4.12.jar"/> | ||||
| 	<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/> | ||||
| 	<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.17/log4j-1.2.17.jar"/> | ||||
| 	<classpathentry kind="output" path="target/classes"/> | ||||
| </classpath> | ||||
							
								
								
									
										14
									
								
								xstream-introduction/.project
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								xstream-introduction/.project
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,14 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <projectDescription> | ||||
|   <name>xstream-introduction</name> | ||||
|   <comment>An Introduction To XStream. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment> | ||||
|   <projects/> | ||||
|   <buildSpec> | ||||
|     <buildCommand> | ||||
|       <name>org.eclipse.jdt.core.javabuilder</name> | ||||
|     </buildCommand> | ||||
|   </buildSpec> | ||||
|   <natures> | ||||
|     <nature>org.eclipse.jdt.core.javanature</nature> | ||||
|   </natures> | ||||
| </projectDescription> | ||||
							
								
								
									
										30
									
								
								xstream-introduction/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								xstream-introduction/pom.xml
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,30 @@ | ||||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||||
| 	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||||
| 	<modelVersion>4.0.0</modelVersion> | ||||
| 	<groupId>org.baeldung</groupId> | ||||
| 	<artifactId>xstream-introduction</artifactId> | ||||
| 	<version>0.0.1-SNAPSHOT</version> | ||||
| 	<name>xstream-introduction</name> | ||||
| 	<description>An Introduction To XStream</description> | ||||
| 
 | ||||
| 	<dependencies> | ||||
| 		<dependency> | ||||
| 			<groupId>com.thoughtworks.xstream</groupId> | ||||
| 			<artifactId>xstream</artifactId> | ||||
| 			<version>1.4.5</version> | ||||
| 		</dependency> | ||||
| 
 | ||||
| 		<dependency> | ||||
| 			<groupId>junit</groupId> | ||||
| 			<artifactId>junit</artifactId> | ||||
| 			<version>4.12</version> | ||||
| 		</dependency> | ||||
| 		 | ||||
| 		<dependency> | ||||
| 			<groupId>log4j</groupId> | ||||
| 			<artifactId>log4j</artifactId> | ||||
| 			<version>1.2.17</version> | ||||
| 		</dependency> | ||||
| 	</dependencies> | ||||
| 
 | ||||
| </project> | ||||
| @ -0,0 +1,19 @@ | ||||
| package org.baeldung.initializer; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.XStream; | ||||
| 
 | ||||
| public class SimpleXstreamInitializer { | ||||
| 
 | ||||
| 	private static XStream xtreamInstance; | ||||
| 
 | ||||
| 	public static XStream getXstreamInstance() { | ||||
| 		if (xtreamInstance == null) { | ||||
| 			synchronized (SimpleXstreamInitializer.class) { | ||||
| 				if (xtreamInstance == null) { | ||||
| 					xtreamInstance = new XStream(); | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		return xtreamInstance; | ||||
| 	} | ||||
| } | ||||
| @ -0,0 +1,40 @@ | ||||
| package org.baeldung.pojo; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||
| 
 | ||||
| @XStreamAlias("AddressDetails") | ||||
| public class AddressDetails { | ||||
| 
 | ||||
| 	private String address; | ||||
| 
 | ||||
| 	private String zipcode; | ||||
| 
 | ||||
| 	private List<ContactDetails> contactDetails; | ||||
| 
 | ||||
| 	public String getZipcode() { | ||||
| 		return zipcode; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setZipcode(String zipcode) { | ||||
| 		this.zipcode = zipcode; | ||||
| 	} | ||||
| 
 | ||||
| 	public List<ContactDetails> getContactDetails() { | ||||
| 		return contactDetails; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setContactDetails(List<ContactDetails> contactDetails) { | ||||
| 		this.contactDetails = contactDetails; | ||||
| 	} | ||||
| 
 | ||||
| 	public String getAddress() { | ||||
| 		return address; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setAddress(String address) { | ||||
| 		this.address = address; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,28 @@ | ||||
| package org.baeldung.pojo; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||
| 
 | ||||
| @XStreamAlias("ContactDetails") | ||||
| public class ContactDetails { | ||||
| 
 | ||||
| 	private String mobile; | ||||
| 
 | ||||
| 	private String landline; | ||||
| 
 | ||||
| 	public String getMobile() { | ||||
| 		return mobile; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setMobile(String mobile) { | ||||
| 		this.mobile = mobile; | ||||
| 	} | ||||
| 
 | ||||
| 	public String getLandline() { | ||||
| 		return landline; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setLandline(String landline) { | ||||
| 		this.landline = landline; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,55 @@ | ||||
| package org.baeldung.pojo; | ||||
| 
 | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||
| import com.thoughtworks.xstream.annotations.XStreamImplicit; | ||||
| import com.thoughtworks.xstream.annotations.XStreamOmitField; | ||||
| 
 | ||||
| @XStreamAlias("customer") | ||||
| public class Customer { | ||||
| 
 | ||||
| 	@XStreamOmitField | ||||
| 	private String firstName; | ||||
| 
 | ||||
| 	private String lastName; | ||||
| 
 | ||||
| 	private Date dob; | ||||
| 
 | ||||
| 	@XStreamImplicit | ||||
| 	private List<ContactDetails> contactDetailsList; | ||||
| 
 | ||||
| 	public String getFirstName() { | ||||
| 		return firstName; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setFirstName(String firstName) { | ||||
| 		this.firstName = firstName; | ||||
| 	} | ||||
| 
 | ||||
| 	public String getLastName() { | ||||
| 		return lastName; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setLastName(String lastName) { | ||||
| 		this.lastName = lastName; | ||||
| 	} | ||||
| 
 | ||||
| 	public Date getDob() { | ||||
| 		return dob; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setDob(Date dob) { | ||||
| 		this.dob = dob; | ||||
| 	} | ||||
| 
 | ||||
| 	public List<ContactDetails> getContactDetailsList() { | ||||
| 		return contactDetailsList; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setContactDetailsList(List<ContactDetails> contactDetailsList) { | ||||
| 		this.contactDetailsList = contactDetailsList; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,50 @@ | ||||
| package org.baeldung.pojo; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||
| 
 | ||||
| @XStreamAlias("CustomerAddressDetails") | ||||
| public class CustomerAddressDetails { | ||||
| 
 | ||||
| 	private List<AddressDetails> addressDetails; | ||||
| 	 | ||||
| 	private String firstName; | ||||
| 
 | ||||
| 	private String lastName; | ||||
| 
 | ||||
| 	private int age; | ||||
| 
 | ||||
| 	public String getFirstName() { | ||||
| 		return firstName; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setFirstName(String firstName) { | ||||
| 		this.firstName = firstName; | ||||
| 	} | ||||
| 
 | ||||
| 	public String getLastName() { | ||||
| 		return lastName; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setLastName(String lastName) { | ||||
| 		this.lastName = lastName; | ||||
| 	} | ||||
| 
 | ||||
| 	public int getAge() { | ||||
| 		return age; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setAge(int age) { | ||||
| 		this.age = age; | ||||
| 	} | ||||
| 
 | ||||
| 
 | ||||
| 	public List<AddressDetails> getAddressDetails() { | ||||
| 		return addressDetails; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setAddressDetails(List<AddressDetails> addressDetails) { | ||||
| 		this.addressDetails = addressDetails; | ||||
| 	} | ||||
| } | ||||
| @ -0,0 +1,20 @@ | ||||
| package org.baeldung.pojo; | ||||
| 
 | ||||
| import java.util.List; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.annotations.XStreamAlias; | ||||
| 
 | ||||
| @XStreamAlias("CustomerPortfolio") | ||||
| public class CustomerPortfolio { | ||||
| 
 | ||||
| 	private List<CustomerAddressDetails> customerAddressDetailsList; | ||||
| 
 | ||||
| 	public List<CustomerAddressDetails> getCustomerAddressDetailsList() { | ||||
| 		return customerAddressDetailsList; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setCustomerAddressDetailsList(List<CustomerAddressDetails> customerAddressDetailsList) { | ||||
| 		this.customerAddressDetailsList = customerAddressDetailsList; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,40 @@ | ||||
| package org.baeldung.utility; | ||||
| 
 | ||||
| import java.text.ParseException; | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Date; | ||||
| import java.util.GregorianCalendar; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.converters.ConversionException; | ||||
| import com.thoughtworks.xstream.converters.Converter; | ||||
| import com.thoughtworks.xstream.converters.MarshallingContext; | ||||
| import com.thoughtworks.xstream.converters.UnmarshallingContext; | ||||
| import com.thoughtworks.xstream.io.HierarchicalStreamReader; | ||||
| import com.thoughtworks.xstream.io.HierarchicalStreamWriter; | ||||
| 
 | ||||
| public class MyDateConverter implements Converter { | ||||
| 
 | ||||
| 	private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); | ||||
| 
 | ||||
| 	@Override | ||||
| 	public boolean canConvert(Class clazz) { | ||||
| 		return Date.class.isAssignableFrom(clazz); | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public void marshal(Object value , HierarchicalStreamWriter writer , MarshallingContext arg2) { | ||||
| 		Date date = (Date) value; | ||||
| 		writer.setValue(formatter.format(date)); | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public Object unmarshal(HierarchicalStreamReader reader , UnmarshallingContext arg1) { | ||||
| 		GregorianCalendar calendar = new GregorianCalendar(); | ||||
| 		try { | ||||
| 			calendar.setTime(formatter.parse(reader.getValue())); | ||||
| 		} catch (ParseException e) { | ||||
| 			throw new ConversionException(e.getMessage() , e); | ||||
| 		} | ||||
| 		return calendar; | ||||
| 	} | ||||
| } | ||||
| @ -0,0 +1,29 @@ | ||||
| package org.baeldung.utility; | ||||
| 
 | ||||
| import java.text.SimpleDateFormat; | ||||
| import java.util.Date; | ||||
| 
 | ||||
| import org.baeldung.pojo.Customer; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.converters.SingleValueConverter; | ||||
| 
 | ||||
| public class MySingleValueConverter implements SingleValueConverter { | ||||
| 
 | ||||
| 	@Override | ||||
| 	public boolean canConvert(Class clazz) { | ||||
| 		return Customer.class.isAssignableFrom(clazz); | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public Object fromString(String arg0) { | ||||
| 		return null; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public String toString(Object obj) { | ||||
| 		SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); | ||||
| 		Date date = ((Customer) obj).getDob(); | ||||
| 		return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
| @ -0,0 +1,37 @@ | ||||
| package org.baeldung.utility; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| import java.util.Calendar; | ||||
| import java.util.List; | ||||
| 
 | ||||
| import org.baeldung.pojo.ContactDetails; | ||||
| import org.baeldung.pojo.Customer; | ||||
| 
 | ||||
| public class SimpleDataGeneration { | ||||
| 
 | ||||
| 	public static Customer generateData() { | ||||
| 		Customer customer = new Customer(); | ||||
| 		Calendar cal = Calendar.getInstance(); | ||||
| 		cal.set(1986 , 01 , 14); | ||||
| 		customer.setDob(cal.getTime()); | ||||
| 		customer.setFirstName("Xstream"); | ||||
| 		customer.setLastName("Java"); | ||||
| 
 | ||||
| 		List<ContactDetails> contactDetailsList = new ArrayList<ContactDetails>(); | ||||
| 
 | ||||
| 		ContactDetails contactDetails1 = new ContactDetails(); | ||||
| 		contactDetails1.setLandline("0124-2460311"); | ||||
| 		contactDetails1.setMobile("6673543265"); | ||||
| 
 | ||||
| 		ContactDetails contactDetails2 = new ContactDetails(); | ||||
| 		contactDetails2.setLandline("0120-223312"); | ||||
| 		contactDetails2.setMobile("4676543565"); | ||||
| 
 | ||||
| 		contactDetailsList.add(contactDetails1); | ||||
| 		contactDetailsList.add(contactDetails2); | ||||
| 
 | ||||
| 		customer.setContactDetailsList(contactDetailsList); | ||||
| 		return customer; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										16
									
								
								xstream-introduction/src/main/resources/log4j.properties
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								xstream-introduction/src/main/resources/log4j.properties
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,16 @@ | ||||
| # Root logger option | ||||
| log4j.rootLogger=DEBUG, file | ||||
| 
 | ||||
| # Redirect log messages to console | ||||
| # log4j.appender.stdout=org.apache.log4j.ConsoleAppender | ||||
| # log4j.appender.stdout.Target=System.out | ||||
| # log4j.appender.stdout.layout=org.apache.log4j.PatternLayout | ||||
| # log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n | ||||
| 
 | ||||
| # Redirect log messages to a log file, support file rolling. | ||||
| log4j.appender.file=org.apache.log4j.RollingFileAppender | ||||
| log4j.appender.file.File=D:\\Test\\xstream-application.log | ||||
| log4j.appender.file.MaxFileSize=5MB | ||||
| log4j.appender.file.MaxBackupIndex=10 | ||||
| log4j.appender.file.layout=org.apache.log4j.PatternLayout | ||||
| log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n | ||||
| @ -0,0 +1,46 @@ | ||||
| package org.baeldung.utility; | ||||
| 
 | ||||
| import org.baeldung.initializer.SimpleXstreamInitializer; | ||||
| import org.baeldung.pojo.AddressDetails; | ||||
| import org.baeldung.pojo.ContactDetails; | ||||
| import org.baeldung.pojo.Customer; | ||||
| import org.junit.Assert; | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import com.thoughtworks.xstream.XStream; | ||||
| 
 | ||||
| public class XStreamSimpleXmlTest { | ||||
| 
 | ||||
| 	private Customer customer = null; | ||||
| 	private String dataXml = null; | ||||
| 	private XStream xtream = null; | ||||
| 
 | ||||
| 	@Before | ||||
| 	public void dataSetup() { | ||||
| 		customer = SimpleDataGeneration.generateData(); | ||||
| 		xtream = SimpleXstreamInitializer.getXstreamInstance(); | ||||
| 		xtream.processAnnotations(Customer.class); | ||||
| 		xtream.processAnnotations(AddressDetails.class); | ||||
| 		xtream.processAnnotations(ContactDetails.class); | ||||
| 		xtream.omitField(Customer.class , "firstName"); | ||||
| 		xtream.registerConverter(new MyDateConverter()); | ||||
| 		//xtream.registerConverter(new MySingleValueConverter()); | ||||
| 		xtream.aliasField("fn", Customer.class, "firstName"); | ||||
| 		 | ||||
| 		dataXml = xtream.toXML(customer); | ||||
| 		System.out.println(dataXml); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void convertDataToXml() { | ||||
| 		Assert.assertNotNull(dataXml); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	public void convertXmlToObject() { | ||||
| 		customer = (Customer) xtream.fromXML(dataXml); | ||||
| 		Assert.assertNotNull(customer); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user