XStream将xml文件拼成字符串
1.引入的包
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7</version>
</dependency>
2.代码如下
import java.util.List; import com.thoughtworks.xstream.annotations.XStreamAlias; import com.thoughtworks.xstream.annotations.XStreamImplicit; @XStreamAlias("OrderHeader") public class FirstXml { // <Receiver></Receiver>收件人 // <ReceiverEmail></ReceiverEmail>电子邮箱 // <ReceiverPhone></ReceiverPhone>手机号 // <OrderLineItem> // <Sequencenum>1</Sequencenum> 兑换顺序号 // <ProductCode>A</ProductCode>礼品ID // <Points>2000</Points>积分分数 // <QuantityRequested>1</QuantityRequested>数量 // <PointType>Point</PointType>分数类型 // </OrderLineItem> // <OrderLineItem> // <Sequencenum>2</Sequencenum> 兑换顺序号 // <ProductCode>B</ProductCode>礼品ID // <Points>3000</Points>积分分数 // <QuantityRequested>1</QuantityRequested>数量 // <PointType>Point</PointType>分数类型 // </OrderLineItem> @XStreamAlias("Receiver")protected String Receiver ;//收件人 @XStreamAlias("ReceiverEmail")protected String ReceiverEmail ;//电子邮箱 @XStreamAlias("ReceiverPhone")protected String ReceiverPhone ;//手机号 @XStreamImplicit(itemFieldName="OrderLineItem") protected List<TwoXml> OrderLineItem; public String getReceiver() { return Receiver; } public void setReceiver(String receiver) { Receiver = receiver; } public String getReceiverEmail() { return ReceiverEmail; } public void setReceiverEmail(String receiverEmail) { ReceiverEmail = receiverEmail; } public String getReceiverPhone() { return ReceiverPhone; } public void setReceiverPhone(String receiverPhone) { ReceiverPhone = receiverPhone; } public List<TwoXml> getOrderLineItem() { return OrderLineItem; } public void setOrderLineItem(List<TwoXml> orderLineItem) { OrderLineItem = orderLineItem; } }
import com.thoughtworks.xstream.annotations.XStreamAlias; @XStreamAlias("OrderLineItem") public class TwoXml { // <OrderLineItem> // <Sequencenum>2</Sequencenum> 兑换顺序号 // <ProductCode>B</ProductCode>礼品ID // <Points>5000</Points>积分分数 // <QuantityRequested>1</QuantityRequested>数量 // <PointType>Point</PointType>分数类型 // </OrderLineItem> @XStreamAlias("Sequencenum")protected int Sequencenum; @XStreamAlias("ProductCode")protected String ProductCode ; @XStreamAlias("Points")protected String Points ; @XStreamAlias("QuantityRequested")protected int QuantityRequested ; @XStreamAlias("PointType")protected String PointType ; public int getSequencenum() { return Sequencenum; } public void setSequencenum(int sequencenum) { Sequencenum = sequencenum; } public String getProductCode() { return ProductCode; } public void setProductCode(String productCode) { ProductCode = productCode; } public String getPoints() { return Points; } public void setPoints(String points) { Points = points; } public int getQuantityRequested() { return QuantityRequested; } public void setQuantityRequested(int quantityRequested) { QuantityRequested = quantityRequested; } public String getPointType() { return PointType; } public void setPointType(String pointType) { PointType = pointType; } }
import java.util.ArrayList; import java.util.List; import com.thoughtworks.xstream.XStream; public class Test { private static XStream xstream = null; public static void main(String[] args) { xstream = new XStream(); FirstXml firstXml = new FirstXml (); firstXml.setReceiver("张三");//姓名 firstXml.setReceiverEmail("123@qq.com");//邮编 firstXml.setReceiverPhone("13340118888");//电话号码 List<TwoXml> aaa = new ArrayList<TwoXml>(); TwoXml twoXml01=new TwoXml(); twoXml01.setSequencenum(1); twoXml01.setProductCode("A"); twoXml01.setPoints("2000"); twoXml01.setQuantityRequested(1); twoXml01.setPointType("Point"); aaa.add(twoXml01); TwoXml twoXml02=new TwoXml(); twoXml02.setSequencenum(2); twoXml02.setProductCode("B"); twoXml02.setPoints("3000"); twoXml02.setQuantityRequested(1); twoXml02.setPointType("Point"); aaa.add(twoXml02); firstXml.setOrderLineItem(aaa); xstream.processAnnotations(FirstXml.class); String inputXml = xstream.toXML(firstXml); System.out.println(inputXml); } }
3.运行结果如下图所示