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. 怎么限制Google自动调整字体大小

    Google默认的字体大小是12px,当样式表中font-size<12px时,或者没有明确指定字体大小,则在chrome浏览器里字体显示是12px. 最近在写代码玩的时候,我也碰到了 在FF和 ...

  2. python 网络编程(四)---UDP服务端客户端

    1.服务器端 UDP服务器建立与TCP相类似,具体比较如下: 补充下,第四步:不必使用listen还有accept函数. 具体代码如下:(设置socket选项省略) import socket fro ...

  3. flappy pig小游戏源码分析(2)——解剖option

    今天继续分析flappy bird的源码.重温一下源码的目录结构. 在本系列第一篇中我们分析了game.js文件,也就是整个程序的架构.这一篇我们来看看option.js文件,这个文件的内容很简单,主 ...

  4. Visual Studio 2015 编译错误【错误 C4996 'vsprintf': This function or variable may be unsafe. Consider using vsprintf_s instead. 】的解决方案

    错误提示信息: 错误 C4996 'vsprintf': This function or variable may be unsafe. Consider using vsprintf_s inst ...

  5. 【C语言】-循环结构-while语句

    while语句: while循环可实现"当型"循环,即当条件成立时,执行循环体.适合无法确定循环执行次数的情况. while (表达式) { 循环语句 } 注意: 表达式为循环控制 ...

  6. semget() semop()

    semget() 可以使用系统调用semget()创建一个新的信号量集,或者存取一个已经存在的信号量集: 系统调用:semget();原型:intsemget(key_t key,int nsems, ...

  7. 转载 GUID介绍

    转载 http://www.cnblogs.com/illele/archive/2008/02/25/1080554.html GUID(Global unique identifier)全局唯一标 ...

  8. 转载 在.net中使用GAC

    转载出处 https://blog.log4d.com/2011/01/gac/ GAC GAC是什么?是用来干嘛的?GAC的全称叫做全局程序集缓存,通俗的理解就是存放各种.net平台下面需要使用的d ...

  9. 牛一网ecshop综合类模板(仿淘常州) for ecshop 2.7.3

    牛一网目前已经开发好这款ecshop综合类/多色系模板,已经开发好多种色系,本模板将会搭配多种颜色让用户选择,以适应 家电数码. 个护化妆. 生鲜水果 . 服饰鞋帽 . 图书音像 . 粮油食品.家具家 ...

  10. Linux协议栈代码阅读笔记(二)网络接口的配置

    Linux协议栈代码阅读笔记(二)网络接口的配置 (基于linux-2.6.11) (一)用户态通过C库函数ioctl进行网络接口的配置 例如,知名的ifconfig程序,就是通过C库函数sys_io ...