Total Pageviews

2016/05/03

[XStream] How to create XML via XStream

Requirement
If we would like to generate a xml file like this:
1
2
3
4
5
6
7
<doc>
  <regId>123456</regId>
  <ownerId>A123456789</ownerId>
  <policyNumber>TA800000001</policyNumber>
  <insuredId>B123456789</insuredId>
  <formId>A00123</formId>
</doc>

How to do it?


How-to
We can make good use of XStream to help us to implement this requirement easily.

Add XStream dependency to your pom.xml
1
2
3
4
5
        <dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.8</version>
        </dependency>


Step1. Create a simple class to correspond to xml tags.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package albert.practice.xstream.beans;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Doc implements Serializable {

 private static final long serialVersionUID = 1L;

 // 訂單編號
 private String regId;

 // 要保人ID
 private String ownerId;

 // 保單號碼
 private String policyNumber;

 // 被保人 ID
 private String insuredId;

 // 表單代碼
 private String formId;

 /**
  * Gets the reg id.
  * 
  * @return the reg id
  */
 public String getRegId() {
  return regId;
 }

 /**
  * Sets the reg id.
  * 
  * @param regId
  *            the new reg id
  */
 public void setRegId(String regId) {
  this.regId = regId;
 }

 /**
  * Gets the owner id.
  * 
  * @return the owner id
  */
 public String getOwnerId() {
  return ownerId;
 }

 /**
  * Sets the owner id.
  * 
  * @param ownerId
  *            the new owner id
  */
 public void setOwnerId(String ownerId) {
  this.ownerId = ownerId;
 }

 /**
  * Gets the policy number.
  * 
  * @return the policy number
  */
 public String getPolicyNumber() {
  return policyNumber;
 }

 /**
  * Sets the policy number.
  * 
  * @param policyNumber
  *            the new policy number
  */
 public void setPolicyNumber(String policyNumber) {
  this.policyNumber = policyNumber;
 }

 /**
  * Gets the insured id.
  * 
  * @return the insured id
  */
 public String getInsuredId() {
  return insuredId;
 }

 /**
  * Sets the insured id.
  * 
  * @param insuredId
  *            the new insured id
  */
 public void setInsuredId(String insuredId) {
  this.insuredId = insuredId;
 }

 /**
  * Gets the form id.
  * 
  * @return the form id
  */
 public String getFormId() {
  return formId;
 }

 /**
  * Sets the form id.
  * 
  * @param formId
  *            the new form id
  */
 public void setFormId(String formId) {
  this.formId = formId;
 }

 /**
  * {@inheritDoc}
  */
 @Override
 public String toString() {
  return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
 }

}


Step2. Serialize object to XML via XStream API
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package albert.practice.xstream;

import com.thoughtworks.xstream.XStream;

import albert.practice.xstream.beans.Doc;

public class XStreamTest2 {

 public static void main(String[] args) {

  // prepare bean for XML
  Doc doc = new Doc();
  doc.setRegId("123456");
  doc.setOwnerId("A123456789");
  doc.setPolicyNumber("TA800000001");
  doc.setInsuredId("B123456789");
  doc.setFormId("A00123");

  // Initializing XStream
  XStream xStream = new XStream();

  // create an alias called doc to the desired class (Doc)
  xStream.alias("doc", Doc.class);

  // Serializing an object to XML
  String xml = xStream.toXML(doc);

  // print XML
  System.out.println(xml);
 }

}


Reference
[1] http://x-stream.github.io/tutorial.html

No comments: