xStream实现Java类输出xml文件

很多情况下,需要生成结构明确的xml文件,来进行接口交互。
生成xml文件有很多工具,Java自己也可以一个节点一个节点的来生成,但效率低下,不方便结构化,也不通用。
可以根据java类来自动生成对应结构化的xml文件的库很多,今天简单说一下xStream库,非常简单,但实用。
如果只是根据java类生成xml文件,只需要xStream.jar以及所以来到xmlpull.jar,我用的是1.4.9版本的库,对应的文件是:xstream-1.4.9.jar以及xmlpull-1.1.3.1.jar。只需要在工程中包含这俩个jar文件即可。

假设要生成的xml结构如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<ADI>
<Asset>
<Metadata>
<AMS Provider="Rojao_IAS" Asset_ID="ADVV2001081701004000" Asset_Class="title" Verb="CREATE" />
<App_Data App="MOD" Value="title"/>
<App_Data App="MOD" Value="2016"/>
</Metadata>
<Asset>
<Metadata>
<AMS Provider="Rojao_IAS" Asset_ID="ADVV2001081701004000" Asset_Class="movie" Verb="CREATE" />
<App_Data App="MOD" Value="N"/>
<App_Data App="MOD" Value="movie"/>
<App_Data App="MOD" Value="0"/>
</Metadata>
<Content Value="ADVV2001081701004000.mpg"/>
</Asset>
</Asset>
</ADI>

那我们可以定义如下的java类:

xStream实现Java类输出xml文件

ADI.java:
public class ADI {
private Asset Asset;
public ADI() {
super();
}
public Asset getAsset() {
return Asset;
}
public void setAsset(Asset asset) {
this.Asset = asset;
}
}


Asset.java:
public class Asset {
private Metadata Metadata;
private Asset Asset;
private Content Content;
public Asset() {
super();
}
public Metadata getMetadata() {
return Metadata;
}
public void setMetadata(Metadata metadata) {
this.Metadata = metadata;
}
public Asset getAsset() {
return Asset;
}
public void setAsset(Asset asset) {
this.Asset = asset;
}
public Content getContent() {
return Content;
}
public void setContent(Content content) {
this.Content = content;
}
}


Metadata.java:
public class Metadata {
private AMS AMS;
private List<AppData> appDatas;
public Metadata() {
super();
this.appDatas = new ArrayList<AppData>();
}
public AMS getAms() {
return AMS;
}
public void setAms(AMS ams) {
this.AMS = ams;
}
public List<AppData> getAppDatas() {
return appDatas;
}
public void setAppDatas(List<AppData> appDatas) {
this.appDatas = appDatas;
}
}


Content.java:


public class Content {
private String Value;
public Content(String value) {
super();
Value = value;
}


public String getValue() {
return Value;
}


public void setValue(String value) {
Value = value;
}
}


AMS.java:


public class AMS {
private String Provider = "Rojao_IAS";
//资产ID
@XStreamAlias("Asset_ID")
private String assetID;
//title|movie
@XStreamAlias("Asset_Class")
private String assetClass;
//CREATE|DELETE
private String Verb;
public AMS(String asset_ID, String asset_Class, String verb) {
super();
assetID = asset_ID;
assetClass = asset_Class;
Verb = verb;
}
public String getProvider() {
return Provider;
}
public void setProvider(String provider) {
Provider = provider;
}
public String getAssetID() {
return assetID;
}
public void setAssetID(String asset_ID) {
assetID = asset_ID;
}
public String getAssetClass() {
return assetClass;
}
public void setAssetClass(String asset_Class) {
assetClass = asset_Class;
}
public String getVerb() {
return Verb;
}
public void setVerb(String verb) {
Verb = verb;
}
}


AppData.java:
public class AppData {


private String App = "MOD";
private String Name;
private String Value;
private String Language;
public AppData(String name, String value) {
super();
Name = name;
Value = value;
}
public String getApp() {
return App;
}
public void setApp(String app) {
App = app;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getValue() {
return Value;
}
public void setValue(String value) {
Value = value;
}
public String getLanguage() {
return Language;
}
public void setLanguage(String language) {
Language = language;
}
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/c45b293a6cce8e122f42f77f454115f3.html