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.运行结果如下图所示

XStream将xml文件拼成字符串的更多相关文章

  1. 读取xml文件转成List<T>对象的两种方法(附源码)

    读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...

  2. 取xml文件转成List<T>对象的两种方法

    读取xml文件转成List<T>对象的两种方法(附源码)   读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...

  3. json串转化成xml文件、xml文件转换成json串

    1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...

  4. 【转】xml节点解析成字符串的方法

    网址:http://blog.csdn.net/shanzhizi/article/details/8817532 ZC: 这是 libxml2的 之前汇总了一篇关于xml文档与字符串转换的文章,文章 ...

  5. (转) 读取xml文件转成List<T>对象的两种方法

    读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法,加上自己知道的另一种实现方法. 就以一个简单的xml做例子. xml格 ...

  6. js将xml对象,xml文件解析成xml dom对象,来对对象进行操作

    由于ie与其他的浏览器对于xml文件的解析方式不同,所以有不同的解析方式 1.1 IE解析xml文件的方式 var xmlDoc=new ActiveXObject("Microsoft.X ...

  7. Xstream 解析xml文件内容

    刚刚接手的一个项目,接到一个对接用户数据的需求,对方使用的是xml格式来传输文件,特此记下解析该类文件的方法 public interface XmlResolver<T> { XStre ...

  8. 通过python将xml文件转换成html文件

    #数据类型的转换 def main():    maxwidth = 100  #用于规范字段的长度    print_start()    count=0    while True:        ...

  9. ORACLE:一列的多行数据拼成字符串

    查询表中的一个字段,返回了多行,就把这么多行的数据都拼成一个字符串. 例:   id  name        1   aa        2   bb        3   cc 要的结果是&quo ...

随机推荐

  1. 选择select框跳出信息

    <html > <body > <select type="select" name=s1 onChange=alert("你选择了&quo ...

  2. 【js与jquery】电子邮箱、手机号、邮政编码的正则验证

    //验证邮政编码 $("#postcode").blur(function(){ //获取邮政编码 var postcode=$("#postcode").va ...

  3. HDU 1078 FatMouse and Cheese 记忆化搜索DP

    直接爆搜肯定超时,除非你加了某种凡人不能想出来的剪枝...555 因为老鼠的路径上的点满足是递增的,所以满足一定的拓补关系,可以利用动态规划求解 但是复杂的拓补关系无法简单的用循环实现,所以直接采取记 ...

  4. RhinoMocks简单范例

    using System; namespace MockTest { public interface IBBB { int Number { get; } int Compute(int j); i ...

  5. Base-Android快速开发框架(三)--数据存储之SQLite

    SQLite,是一款轻量级的关系型数据库,Android原生集成的一个数据库.具有轻量级.独立性.隔离性.安全性等特点.是Android做数据存储的必备知识之一. 在实际的项目中,我们常用于一些对象的 ...

  6. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  7. 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数

    题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次. 分析:首先最先想到的是遍历从1到n的每 ...

  8. TCP/IP 相关总结

    1.   三次握手协议 在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND状态,等 ...

  9. 一些好用的nginx第三方模块

    一些好用的nginx第三方模块 转自;http://macken.iteye.com/blog/1963301  1.Development Kit https://github.com/simpl/ ...

  10. 秀一套每秒处理1500+个事务的profile

    秀一套每秒处理1500+个事务的profile,真实生产环境