xml tutorials
This commit is contained in:
@@ -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>
|
||||
<parent>
|
||||
<groupId>net.aimeizi</groupId>
|
||||
<artifactId>xml-tutorials</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<artifactId>xml-xstream-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<artifactId>junit</artifactId>
|
||||
<groupId>junit</groupId>
|
||||
<version>4.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.thoughtworks.xstream</groupId>
|
||||
<artifactId>xstream</artifactId>
|
||||
<version>1.4.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.jettison</groupId>
|
||||
<artifactId>jettison</artifactId>
|
||||
<version>1.3.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package net.aimeizi.xstream.annotation.bean2xml.example;
|
||||
|
||||
import net.aimeizi.xstream.annotation.example.Person;
|
||||
import net.aimeizi.xstream.annotation.example.PhoneNumber;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/annotations-tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
public class SerializingObject2XML {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
XStream xstream = new XStream();
|
||||
//XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
|
||||
//XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
|
||||
|
||||
xstream.processAnnotations(Person.class);
|
||||
|
||||
Person joe = new Person("Joe", "Walnes");
|
||||
joe.setPhone(new PhoneNumber(123, "1234-456"));
|
||||
joe.setFax(new PhoneNumber(123, "9999-999"));
|
||||
|
||||
String xml = xstream.toXML(joe);
|
||||
|
||||
System.out.println(xml);
|
||||
}
|
||||
|
||||
}
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
package net.aimeizi.xstream.annotation.example;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/annotations-tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
|
||||
@XStreamAlias("person")
|
||||
public class Person {
|
||||
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
@XStreamAlias("phone")
|
||||
private PhoneNumber phone;
|
||||
@XStreamAlias("fax")
|
||||
private PhoneNumber fax;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstname, String lastname) {
|
||||
super();
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public Person(String firstname, String lastname, PhoneNumber phone,
|
||||
PhoneNumber fax) {
|
||||
super();
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.phone = phone;
|
||||
this.fax = fax;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 PhoneNumber getPhone() {
|
||||
return phone;
|
||||
}
|
||||
public void setPhone(PhoneNumber phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
public PhoneNumber getFax() {
|
||||
return fax;
|
||||
}
|
||||
public void setFax(PhoneNumber fax) {
|
||||
this.fax = fax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [firstname=" + firstname + ", lastname=" + lastname
|
||||
+ ", phone=" + phone.getNumber() + ", fax=" + fax.getNumber() + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package net.aimeizi.xstream.annotation.example;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/annotations-tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
|
||||
@XStreamAlias("Phonenumber")
|
||||
public class PhoneNumber {
|
||||
|
||||
private int code;
|
||||
private String number;
|
||||
|
||||
public PhoneNumber() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public PhoneNumber(int code, String number) {
|
||||
super();
|
||||
this.code = code;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package net.aimeizi.xstream.annotation.xml2bean.example;
|
||||
|
||||
import net.aimeizi.xstream.annotation.example.Person;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/annotations-tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
public class DeserializingXM2Object {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
XStream xstream = new XStream();
|
||||
//XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
|
||||
//XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
|
||||
|
||||
xstream.processAnnotations(Person.class);
|
||||
|
||||
String xml="<person><firstname>Joe</firstname><lastname>Walnes</lastname><phone><code>123</code><number>1234-456</number></phone><fax><code>123</code><number>9999-999</number></fax></person>";
|
||||
|
||||
Person person = (Person)xstream.fromXML(xml);
|
||||
|
||||
System.out.println(person);
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package net.aimeizi.xstream.bean2xml.example;
|
||||
|
||||
import net.aimeizi.xstream.example.Person;
|
||||
import net.aimeizi.xstream.example.PhoneNumber;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
public class SerializingObject2XML {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
XStream xstream = new XStream();
|
||||
//XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
|
||||
//XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
|
||||
|
||||
xstream.alias("person", Person.class);
|
||||
xstream.alias("phonenumber", PhoneNumber.class);
|
||||
|
||||
Person joe = new Person("Joe", "Walnes");
|
||||
joe.setPhone(new PhoneNumber(123, "1234-456"));
|
||||
joe.setFax(new PhoneNumber(123, "9999-999"));
|
||||
|
||||
String xml = xstream.toXML(joe);
|
||||
|
||||
System.out.println(xml);
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package net.aimeizi.xstream.example;
|
||||
|
||||
public class Person {
|
||||
|
||||
private String firstname;
|
||||
private String lastname;
|
||||
private PhoneNumber phone;
|
||||
private PhoneNumber fax;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String firstname, String lastname) {
|
||||
super();
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
|
||||
public Person(String firstname, String lastname, PhoneNumber phone,
|
||||
PhoneNumber fax) {
|
||||
super();
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.phone = phone;
|
||||
this.fax = fax;
|
||||
}
|
||||
|
||||
|
||||
|
||||
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 PhoneNumber getPhone() {
|
||||
return phone;
|
||||
}
|
||||
public void setPhone(PhoneNumber phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
public PhoneNumber getFax() {
|
||||
return fax;
|
||||
}
|
||||
public void setFax(PhoneNumber fax) {
|
||||
this.fax = fax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [firstname=" + firstname + ", lastname=" + lastname
|
||||
+ ", phone=" + phone.getNumber() + ", fax=" + fax.getNumber() + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package net.aimeizi.xstream.example;
|
||||
|
||||
public class PhoneNumber {
|
||||
|
||||
private int code;
|
||||
private String number;
|
||||
|
||||
public PhoneNumber() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
public PhoneNumber(int code, String number) {
|
||||
super();
|
||||
this.code = code;
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
public String getNumber() {
|
||||
return number;
|
||||
}
|
||||
public void setNumber(String number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package net.aimeizi.xstream.json.example;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class Product implements Serializable{
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public Product(String id, String name, double price) {
|
||||
|
||||
}
|
||||
|
||||
public Product() {
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package net.aimeizi.xstream.json.example;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/json-tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
public class XStreamJson2Object {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String json = "{\"product\":{\"name\":\"Banana\",\"id\":123"
|
||||
+ ",\"price\":23.0}}";
|
||||
XStream xstream = new XStream(new JettisonMappedXmlDriver());
|
||||
xstream.alias("product", Product.class);
|
||||
Product product = (Product)xstream.fromXML(json);
|
||||
System.out.println(product.getName());
|
||||
}
|
||||
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package net.aimeizi.xstream.json.example;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/json-tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
public class XStreamObject2Json {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Product product = new Product();
|
||||
|
||||
product.setId("123");
|
||||
product.setName("Banana");
|
||||
product.setPrice(23.00);
|
||||
|
||||
XStream xstream = new XStream(new JettisonMappedXmlDriver());
|
||||
|
||||
/*XStream xstream = new XStream(new JsonHierarchicalStreamDriver());*/
|
||||
|
||||
/*XStream xstream = new XStream(new JsonHierarchicalStreamDriver() {
|
||||
public HierarchicalStreamWriter createWriter(Writer writer) {
|
||||
return new JsonWriter(writer, JsonWriter.DROP_ROOT_MODE);
|
||||
}
|
||||
});*/
|
||||
|
||||
xstream.setMode(XStream.NO_REFERENCES);
|
||||
xstream.alias("product", Product.class);
|
||||
System.out.println(xstream.toXML(product));
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package net.aimeizi.xstream.xml2bean.example;
|
||||
|
||||
import net.aimeizi.xstream.example.Person;
|
||||
import net.aimeizi.xstream.example.PhoneNumber;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
/**
|
||||
* @see http://xstream.codehaus.org/tutorial.html
|
||||
* @author scott
|
||||
*
|
||||
*/
|
||||
public class DeserializingXM2Object {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
XStream xstream = new XStream();
|
||||
//XStream xstream = new XStream(new DomDriver()); // does not require XPP3 library
|
||||
//XStream xstream = new XStream(new StaxDriver()); // does not require XPP3 library starting with Java 6
|
||||
|
||||
xstream.alias("person", Person.class);
|
||||
xstream.alias("phonenumber", PhoneNumber.class);
|
||||
|
||||
String xml="<person><firstname>Joe</firstname><lastname>Walnes</lastname><phone><code>123</code><number>1234-456</number></phone><fax><code>123</code><number>9999-999</number></fax></person>";
|
||||
|
||||
Person person = (Person)xstream.fromXML(xml);
|
||||
|
||||
System.out.println(person);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user