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. JDBC基础教程

    本文实例讲述了JDBC基础知识与技巧.分享给大家供大家参考.具体分析如下: 1.什么是JDBC? 通俗来讲JDBC技术就是通过java程序来发送SQL语句到数据库,数据库收到SQL语句后执行,把结果返 ...

  2. CF GYM 100703L Many questions

    题意:题意真坑……龙要问一系列问题,王子骑士公主分别以一个整数回答,如果王子和公主答案差的绝对值比骑士和公主答案差的绝对值小则说王子和公主的答案更相似,反过来如果前者比后者大则说骑士和公主的答案更相似 ...

  3. ZOJ 3264 Present for MM

    寒假开始了···但是做题没有结束嘤··· 然后是dp专场嘤··· 题意:背包问题,给出背包容量和物品对数,每对物品都有特殊的关系:第一种关系是两个物品都取有价值,若只取一个则失去价值:第二种是两个物品 ...

  4. hbase shell下如何使用删除键

    今天刚安装好了hbase,通过Secure CRT登录hbase shell,敲入错误命令无法使用删除键(Backspace或是Ctrl+Backspace都不管用)删除,后来在终端-->仿真下 ...

  5. alibaba笔试1

    5.D 一个线程不可以改变另一个线程的程序计数器.如果改变了,线程在切换后就恢复不到正确的位置. 一个线程可以访问另一个线程的栈.http://bbs.csdn.net/topics/39008942 ...

  6. codeforces 672C - Recycling Bottles 贪心水题

    感觉很简单,就是讨论一下 #include <stdio.h> #include <string.h> #include <algorithm> #include ...

  7. 《Python基础教程(第二版)》学习笔记 -> 第一章 基础知识

    写笔记的原因:书也看了一遍,视频也看了,但总是感觉效果不好,一段时间忘记了,再看又觉得有心无力,都是PDF的书籍,打开了就没有心情了,上班一天了,回家看这些东西,真的没多大精力了,所以,我觉得还是把p ...

  8. 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征

    (一)HOG特征 1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检测的特征描述子.它通过计算和 ...

  9. CentOS无损调整home,root磁盘的大小

    调整硬盘分区大小想增加root空间,减少home空间. 需要说明的是整个操作需要使用root用户. 1.查看硬盘使用情况. [root@Slave1 /]# df -h Filesystem Size ...

  10. 大数据架构师基础:hadoop家族,Cloudera产品系列等各种技术

    大数据我们都知道hadoop,可是还会各种各样的技术进入我们的视野:Spark,Storm,impala,让我们都反映不过来.为了能够更好的架构大数据项目,这里整理一下,供技术人员,项目经理,架构师选 ...