parent
c9fbff7273
commit
f7594ee89c
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>xstream-xmlToObject</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
|
@ -0,0 +1,15 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:05.874 UTC</dob>
|
||||
<contactDetailsList>
|
||||
<ContactDetails contactType="Home">
|
||||
<mobile>6673543265</mobile>
|
||||
<landline>0124-2460311</landline>
|
||||
</ContactDetails>
|
||||
<ContactDetails contactType="Office">
|
||||
<mobile>4676543565</mobile>
|
||||
<landline>0120-223312</landline>
|
||||
</ContactDetails>
|
||||
</contactDetailsList>
|
||||
</customer>
|
|
@ -0,0 +1,5 @@
|
|||
<customer>
|
||||
<fn>XStream</fn>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</customer>
|
|
@ -0,0 +1,13 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||
<ContactDetails contactType="Home">
|
||||
<mobile>6673543265</mobile>
|
||||
<landline>0124-2460311</landline>
|
||||
</ContactDetails>
|
||||
<ContactDetails contactType="Office">
|
||||
<mobile>4676543565</mobile>
|
||||
<landline>0120-223312</landline>
|
||||
</ContactDetails>
|
||||
</customer>
|
|
@ -0,0 +1,5 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</customer>
|
|
@ -0,0 +1,6 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||
<fullName>XStream Java</fullName>
|
||||
</customer>
|
|
@ -0,0 +1,5 @@
|
|||
<com.baeldung.pojo.Customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</com.baeldung.pojo.Customer>
|
|
@ -0,0 +1,36 @@
|
|||
<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>com.baeldung</groupId>
|
||||
<artifactId>xstream-xmlToObject</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>xstream-xmlToObject</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<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,46 @@
|
|||
package com.baeldung.annotation.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
@XStreamAlias("fn")
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.annotation.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamOmitField;
|
||||
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class CustomerOmitField {
|
||||
|
||||
@XStreamOmitField
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName="
|
||||
+ lastName + ", dob=" + dob + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.complex.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
@XStreamAlias("ContactDetails")
|
||||
public class ContactDetails {
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String landline;
|
||||
|
||||
@XStreamAsAttribute
|
||||
private String contactType;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(String contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||
+ ", contactType=" + contactType + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.complex.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.implicit.collection.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
@XStreamAlias("ContactDetails")
|
||||
public class ContactDetails {
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String landline;
|
||||
|
||||
@XStreamAsAttribute
|
||||
private String contactType;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(String contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||
+ ", contactType=" + contactType + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.implicit.collection.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.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,42 @@
|
|||
package com.baeldung.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.utility;
|
||||
|
||||
import java.util.Calendar;
|
||||
|
||||
import com.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");
|
||||
|
||||
return customer;
|
||||
}
|
||||
}
|
|
@ -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,38 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.complex.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class ComplexXmlToObjectAnnotationTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-alias-field-complex.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.implicit.collection.pojo.ContactDetails;
|
||||
import com.baeldung.implicit.collection.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class ComplexXmlToObjectAttributeCollectionTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
for(ContactDetails contactDetails : customer.getContactDetailsList()){
|
||||
Assert.assertNotNull(contactDetails.getContactType());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.implicit.collection.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class ComplexXmlToObjectCollectionTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-alias-implicit-collection.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
System.out.println(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectAliasTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer" , Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-alias.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.annotation.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectAnnotationTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-alias-field.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getFirstName());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectFieldAliasTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer" , Customer.class);
|
||||
xstream.aliasField("fn", Customer.class, "firstName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-alias-field.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getFirstName());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectIgnoreFieldsTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer" , Customer.class);
|
||||
xstream.ignoreUnknownElements();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file-ignore-field.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
System.out.println(customer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.baeldung.utility.SimpleDataGeneration;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
xstream = SimpleXstreamInitializer.getXstreamInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
FileReader reader = new FileReader(new File("data-file.xml"));
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromString() {
|
||||
Customer customer = SimpleDataGeneration.generateData();
|
||||
String dataXml = xstream.toXML(customer);
|
||||
Customer convertedCustomer = (Customer) xstream.fromXML(dataXml);
|
||||
Assert.assertNotNull(convertedCustomer);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue