xstream 使用方式
xml文件内容:
<list>
<storagePool>
<name>isopool</name>
<path>/vms/isos</path>
<type>dir</type>
<totalSize>4983</totalSize>
<freeSize>4810</freeSize>
<status>1</status>
</storagePool>
<storagePool>
<name>defaultpool</name>
<path>/vms/images</path>
<type>dir</type>
<totalSize>4983</totalSize>
<freeSize>4771</freeSize>
<status>1</status>
</storagePool>
</list>
//测试代码
public static void main(String[] args) throws IOException {
//由XML转化成JavaBean
String filePath = "E:\\杂乱代码\\store.xml";
String xml = FileUtil.readFileContent(filePath);
System.out.println(xml);
XStream xstream = new XStream(new DomDriver());
xstream.processAnnotations(H3CVMStoragePoolList.class);//必须把根目录注册进去
H3CVMStoragePoolList h3CVMStoragePoolList = (H3CVMStoragePoolList) xstream.fromXML(xml);
for(H3CVMStoragePool temp : h3CVMStoragePoolList.getList())
{
System.out.println(temp.getName());
}
//由bean转化成xml的String
H3CVolumAdd h3CVolumAdd = new H3CVolumAdd();
h3CVolumAdd.setHostId(1124);
h3CVolumAdd.setSpName("/df/df/df/df");
h3CVolumAdd.setVolName("/fg/fg/fg");
h3CVolumAdd.setBaseFile("/fgh/ghgfh");
XStream xstream1 = new XStream(new DomDriver());
//使用@XStreamOmitField对不生成xml的字段加注释
//然后代码中使用xstream.autodetectAnnotations(true);就可以完成去掉属性了
xstream1.autodetectAnnotations(true);
String volAddXml = xstream1.toXML(h3CVolumAdd);
System.out.println(volAddXml);
}
@XStreamAlias("list")//这个注释必须有
public class H3CVMStoragePoolList implements Serializable{
private static final long serialVersionUID = 1L;
@XStreamImplicit(itemFieldName="storagePool")//这个注释必须有
private ArrayList<H3CVMStoragePool> list;
public ArrayList<H3CVMStoragePool> getList() {
return list;
}
public void setList(ArrayList<H3CVMStoragePool> list) {
this.list = list;
}
}
@XStreamAlias("storagePool") //这个注释必须有
public class H3CVMStoragePool implements Serializable{
private static final long serialVersionUID = 1L;
String name;
String path;
String type;
long totalSize;
long freeSize;
int status;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getTotalSize() {
return totalSize;
}
public void setTotalSize(long totalSize) {
this.totalSize = totalSize;
}
public long getFreeSize() {
return freeSize;
}
public void setFreeSize(long freeSize) {
this.freeSize = freeSize;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}
@XStreamAlias("volAddParameter")//这个注释信息必须有的
public class H3CVolumAdd {
private long hostId;
private String spName;
private String volName;
private long capacity;
private String format;
//使用@XStreamOmitField对不生成xml的字段加注释
//然后代码中使用xstream.autodetectAnnotations(true);就可以完成去掉属性了
@XStreamOmitField
private String baseFile;
public long getHostId() {
return hostId;
}
public void setHostId(long hostId) {
this.hostId = hostId;
}
public String getSpName() {
return spName;
}
public void setSpName(String spName) {
this.spName = spName;
}
public String getVolName() {
return volName;
}
public void setVolName(String volName) {
this.volName = volName;
}
public long getCapacity() {
return capacity;
}
public void setCapacity(long capacity) {
this.capacity = capacity;
}
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
public String getBaseFile() {
return baseFile;
}
public void setBaseFile(String baseFile) {
this.baseFile = baseFile;
}
}
- 使用XStream处理Xml文档和实体类之间的转换的几种方式
- XStream的使用(4)—— 注解方式
- 解析XML文件(一)----使用XStream方式
- XStream使用注解的方式格式化生成的xml
- 使用命名管道的OVERLAPPED方式实现非阻塞模式编程
- native2ascii.exe使用方式
- asp.net中使用自定义控件的方式实现一个分页控件的代码
- Spring @Async配置4. 基于@Async无返回值调用 使用的方式非常简单,一个标注即可解决所有的问题: 1 @Async //标注使用 2 public void asyncMe
- gdbserver的安装和使用 (方式2)
- 实验四——使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
- linux进程间通信,使用共享内存方式
- linux内存使用计算方式
- VC++中使用ADO方式操作ACCESS数据库
- 关于本地管理的Extent分配使用 Uniform 方式时,如何设置这个数字
- 走进AngularJs(二) ng模板中常用指令的使用方式
- 使用window.open打开新窗口,以post方式传参数
- Android5.0特性 - TextInputLayout的三种使用方式
- 使用Qt Designer和手动编写代码两种方式实现多窗口切换程序(Qt基础)
- 使用编程的方式来启动SharePoint的工作流 并传入参数
- VB 驱动加载类(直接使用NtLoadDriver加载方式)