The XSD for xml is as following:
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 | <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Root"> <xs:complexType> <xs:sequence> <xs:element name="Attributes"> <xs:complexType> <xs:sequence> <xs:element type="xs:string" name="WorkLoad"/> <xs:element type="xs:string" name="DetailDocCat"/> <xs:element type="xs:string" name="ScanDate"/> <xs:element type="xs:time" name="ScanTime"/> <xs:element type="xs:float" name="StockingNumber"/> <xs:element type="xs:string" name="CaseId"/> <xs:element name="PolicyNumbers"> <xs:complexType> <xs:sequence> <xs:element type="xs:string" name="PolicyNumber" maxOccurs="unbounded" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="ProposalNums"> <xs:complexType> <xs:sequence> <xs:element type="xs:string" name="ProposalNum" maxOccurs="unbounded" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> |
The resulting XML looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <Root> <Attributes> <WorkLoad>TW_NEWNBED</WorkLoad> <DetailDocCat>xxx要保書</DetailDocCat> <ScanDate>2016/03/02</ScanDate> <ScanTime>15:56:00</ScanTime> <StockingNumber>2016E000001</StockingNumber> <CaseId>ABCD-201600100001</CaseId> <PolicyNumbers> <PolicyNumber>A12345678</PolicyNumber> </PolicyNumbers> <ProposalNums> <ProposalNum>B87654321</ProposalNum> </ProposalNums> </Attributes> </Root> |
How-to
Step1. According xsd definition, creating classes to be serialized
Root class looks like this:
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 | 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 Root implements Serializable { private static final long serialVersionUID = 1L; private Attributes Attributes; public Attributes getAttributes() { return Attributes; } public void setAttributes(Attributes Attributes) { this.Attributes = Attributes; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } |
Attributes class looks like this:
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 | package albert.practice.xstream.beans; import java.io.Serializable; import java.util.List; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; public class Attributes implements Serializable { private static final long serialVersionUID = 1L; private String WorkLoad; private String DetailDocCat; private String ScanDate; private String ScanTime; private String StockingNumber; private String CaseId; private List<PolicyNumbers> PolicyNumbers; private List<ProposalNums> ProposalNums; public String getWorkLoad() { return WorkLoad; } public void setWorkLoad(String WorkLoad) { this.WorkLoad = WorkLoad; } public String getDetailDocCat() { return DetailDocCat; } public void setDetailDocCat(String DetailDocCat) { this.DetailDocCat = DetailDocCat; } public String getStockingNumber() { return StockingNumber; } public void setStockingNumber(String StockingNumber) { this.StockingNumber = StockingNumber; } public List<PolicyNumbers> getPolicyNumbers() { return PolicyNumbers; } public void setPolicyNumbers(List<PolicyNumbers> policyNumbers) { PolicyNumbers = policyNumbers; } public String getCaseId() { return CaseId; } public void setCaseId(String CaseId) { this.CaseId = CaseId; } public String getScanDate() { return ScanDate; } public void setScanDate(String ScanDate) { this.ScanDate = ScanDate; } public String getScanTime() { return ScanTime; } public void setScanTime(String ScanTime) { this.ScanTime = ScanTime; } public List<ProposalNums> getProposalNums() { return ProposalNums; } public void setProposalNums(List<ProposalNums> proposalNums) { ProposalNums = proposalNums; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } |
PolicyNumbers looks like this:
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 | package albert.practice.xstream.beans; import java.io.Serializable; import java.util.List; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; public class PolicyNumbers implements Serializable { private static final long serialVersionUID = 1L; private String PolicyNumber; private List<PolicyNumbers> PolicyNumberList; public List<PolicyNumbers> getPolicyNumberList() { return PolicyNumberList; } public void setPolicyNumberList(List<PolicyNumbers> policyNumberList) { PolicyNumberList = policyNumberList; } public PolicyNumbers(String policyNumber) { this.PolicyNumber = policyNumber; } public String getPolicyNumber() { return PolicyNumber; } public void setPolicyNumber(String policyNumber) { this.PolicyNumber = policyNumber; } public PolicyNumbers() { super(); } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } |
ProposalNumbers looks like this:
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 | package albert.practice.xstream.beans; import java.io.Serializable; import java.util.List; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; public class ProposalNums implements Serializable { private static final long serialVersionUID = 1L; private String ProposalNum; private List<ProposalNums> ProposalNumList; public String getProposalNum() { return ProposalNum; } public void setProposalNum(String proposalNum) { ProposalNum = proposalNum; } public List<ProposalNums> getProposalNumList() { return ProposalNumList; } public void setProposalNumList(List<ProposalNums> proposalNumList) { ProposalNumList = proposalNumList; } public ProposalNums(String proposalNum) { super(); ProposalNum = proposalNum; } public ProposalNums() { super(); } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } } |
Step2. Utilize XStream API to fulfill our requirement
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 | public class XStreamTestClient { public static void main(String[] args) throws IOException { // create an instance of beans and populate its fields: Attributes attribute = new Attributes(); attribute.setWorkLoad("TW_NEWNBED"); attribute.setDetailDocCat("xxx要保書"); attribute.setScanDate("2016/03/02"); attribute.setScanTime("15:56:00"); attribute.setStockingNumber("2016E000001"); attribute.setCaseId("ABCD-201600100001"); List<PolicyNumbers> policyNumberList = new ArrayList<PolicyNumbers>(); policyNumberList.add(new PolicyNumbers("A12345678")); attribute.setPolicyNumbers(policyNumberList); List<ProposalNums> proposalNumList = new ArrayList<ProposalNums>(); proposalNumList.add(new ProposalNums("B87654321")); attribute.setProposalNums(proposalNumList); Root root = new Root(); root.setAttributes(attribute); String xml = new XStreamTestClient().toXml(root); System.out.println(xml); } public String toXml(Root root) { // instantiate the XStream class XStream xStream = new XStream(); // create an alias to the desired class: xStream.alias("Root", Root.class); xStream.alias("PolicyNumbers", PolicyNumbers.class); xStream.alias("ProposalNums", ProposalNums.class); // whenever you have a collection which doesn't need to display it's // root tag, you can map it as an implicit collection xStream.addImplicitCollection(Attributes.class, "PolicyNumbers"); xStream.addImplicitCollection(Attributes.class, "ProposalNums"); return xStream.toXML(root); } } |
No comments:
Post a Comment