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

XStream将xml文件拼成字符串的更多相关文章
- 读取xml文件转成List<T>对象的两种方法(附源码)
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法, ...
- 取xml文件转成List<T>对象的两种方法
读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件转成List<T>对象的两种方法(附源码) 读取xml文件,是项目中经常要用到的,所以就总结一下,最 ...
- json串转化成xml文件、xml文件转换成json串
1.json串转化成xml文件 p=[{"name":"tom","age":30,"sex":"男" ...
- 【转】xml节点解析成字符串的方法
网址:http://blog.csdn.net/shanzhizi/article/details/8817532 ZC: 这是 libxml2的 之前汇总了一篇关于xml文档与字符串转换的文章,文章 ...
- (转) 读取xml文件转成List<T>对象的两种方法
读取xml文件,是项目中经常要用到的,所以就总结一下,最近项目中用到的读取xml文件并且转成List<T>对象的方法,加上自己知道的另一种实现方法. 就以一个简单的xml做例子. xml格 ...
- js将xml对象,xml文件解析成xml dom对象,来对对象进行操作
由于ie与其他的浏览器对于xml文件的解析方式不同,所以有不同的解析方式 1.1 IE解析xml文件的方式 var xmlDoc=new ActiveXObject("Microsoft.X ...
- Xstream 解析xml文件内容
刚刚接手的一个项目,接到一个对接用户数据的需求,对方使用的是xml格式来传输文件,特此记下解析该类文件的方法 public interface XmlResolver<T> { XStre ...
- 通过python将xml文件转换成html文件
#数据类型的转换 def main(): maxwidth = 100 #用于规范字段的长度 print_start() count=0 while True: ...
- ORACLE:一列的多行数据拼成字符串
查询表中的一个字段,返回了多行,就把这么多行的数据都拼成一个字符串. 例: id name 1 aa 2 bb 3 cc 要的结果是&quo ...
随机推荐
- Dos操作
\tree/f >c.txt \dir/s/b >c.txt
- 统计计算与R语言的资料汇总(截止2016年12月)
本文在Creative Commons许可证下发布. 在fedora Linux上断断续续使用R语言过了9年后,发现R语言在国内用的人逐渐多了起来.由于工作原因,直到今年暑假一个赴京工作的机会与一位统 ...
- 使用asp.net MVC4中的Bundle遇到的问题及解决办法
背景 之前有过使用MVC3的经验,也建过MVC4的基本样例看过,知道有bundle这么一个方法. 近日想建个网站使用MVC4,但是我觉得在基本样例上改不好,有太多无用的东西,所以就建了一个空白的MVC ...
- Makefile编程
[个人体会]0.1 项目文件要合理分隔,功能模块分开放,分别设置Makefile自动编译, 0.2 源码和头文件分开放,一个或多个头文件对应一个源码文件. ...
- bzoj 1058 [ZJOI2007]报表统计(set)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1058 [题意] 一个序列,提供插入,查询相邻最小差值,查询任意最小差值的操作. [思路 ...
- Node.js也分裂了-开源社区动态
继CoreOS与Docker分道扬镳继而推出自己的容器引擎Rocket后不久,又传来了Node.js分裂的消息.由于Node.js的贡献者因对负责Node.js开发的公司Joyent在对Node.js ...
- C++11 并发指南------std::thread 详解
参考: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice/blob/master/zh/chapter3-Thread/Int ...
- algorithm@ find kth smallest element in two sorted arrays (O(log n time)
The trivial way, O(m + n): Merge both arrays and the k-th smallest element could be accessed directl ...
- Getty – Java NIO 框架设计与实现
前言 Getty是我为了学习 Java NIO 所写的一个 NIO 框架,实现过程中参考了 Netty 的设计,同时使用 Groovy 来实现.虽然只是玩具,但是麻雀虽小,五脏俱全,在实现过程中,不仅 ...
- 【转】logger
一个使用java.util.logging.Logger进行log输出的示例代码如下: package com.sample; import java.io.File; import java.uti ...