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 ...
随机推荐
- win2003 多域名绑定一个ip
一个IP绑定多个域名 很多虚拟主机,只有一个IP,很多个域名都指向该IP,但都能访问自己域名所在 的网站的内容,这就是一个IP绑定多个域名的技术. 我们得先了解一个概念 什么是主机头所谓的主机头的叫法 ...
- JQuery: 微博演示
演示图: 完成图: 微博演示代码如下: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> ...
- CF GYM 100703B Energy Saving
题意:王子每月买m个灯泡给n个房间换灯泡,如果当前有的灯泡数够列表的第一个房间换的就全换,直到灯泡不够为止,给出q个查询,查询x月已经换好几个房子,手里还剩多少灯泡. 解法:水题……小模拟. 代码: ...
- UVA 10600 ACM Contest and Blackout 次小生成树
又是求次小生成树,就是求出最小生成树,然后枚举不在最小生成树上的每条边,求出包含着条边的最小生成树,然后取一个最小的 #include <iostream> #include <al ...
- <转>堆和栈的区别
http://blog.csdn.net/hairetz/article/details/4141043 一.预备知识—程序的内存分配 一个由C/C++编译的程序占用的内存分为以下几个部分 ...
- tomcat 7 中的类加载器学习
tomcat 7自带很多junit测试用例,可以帮助我们窥探源码的秘密.以下使用来测试类加载器的一个测试用例.类加载器也是对象,他们用来将类从类从.class文件加载到虚拟机,这些已经讲了很多,深入j ...
- ubuntu安装和卸载软件命令
ubuntu安装和卸载软件命令 Ubuntu软件安装与删除相关命令 安装软件 命令: apt-get install softname1 softname2 softname3…… 卸载软件 命令: ...
- HIbernate学习笔记(六) 关系映射之多对多
六.多对多 - 单向 Ø 一般的设计中,多对多关联映射,需要一个中间表 Ø Hibernate会自动生成中间表 Ø Hibernate使用many-to-ma ...
- BNUOJ-26580 Software Bugs KMP匹配,维护
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26580 题意:给一个模式串,然后m个匹配串,要求删掉匹配串中的所有存在的模式串,使得余下的 ...
- [坑]c#中double转字符串精度丢失问题记录
在项目遇到了一个比较大的double值,然后出现了一些意想不到的状况: double b=1141.161994934082; b.ToString();//'1141.16199493408' 然后 ...