歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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" Name="Type" Value="title"/>
<App_Data App="MOD" Name="Year" Value="2016"/>
</Metadata>
<Asset>
<Metadata>
<AMS Provider="Rojao_IAS" Asset_ID="ADVV2001081701004000" Asset_Class="movie" Verb="CREATE" />
<App_Data App="MOD" Name="Encryption" Value="N"/>
<App_Data App="MOD" Name="Type" Value="movie"/>
<App_Data App="MOD" Name="Status" Value="0"/>
</Metadata>
<Content Value="ADVV2001081701004000.mpg"/>
</Asset>
</Asset>
</ADI>

那我們可以定義如下的java類:

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;
}
}


從上面的類的結構可以看出,通過類的包含關系,分別對應到xml格式的對應元素上。
通過注解,可以設置屬性字段的名稱,如@XStreamAlias
ADI adi = new ADI();
........設置ADI類以及子類的所有屬性


XStream xstream = new XStream(new XppDriver(new NoNameCoder()));
使用NoNameCoder,是為了避免帶下劃線的字段,在生成xml時被自動替換為雙下劃線的問題。
try {
PrintWriter writer = new PrintWriter("f:/ADVV2001081701004000.xml","utf-8");
//自己寫xml頭部
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
//設置Xstream解析注解
xstream.autodetectAnnotations(true);
//String類型字段解析為xml屬性而為子節點
xstream.useAttributeFor(String.class);
//設置類的別名
xstream.alias("ADI", ADI.class);
xstream.alias("App_Data", AppData.class);
//忽略集合名稱,也就是生成的xml中不包含集合名稱的節點,而是把集合元素輸出
xstream.addImplicitCollection(Metadata.class, "appDatas");
xstream.toXML(adi, writer);
} catch (Exception e) {
e.printStackTrace();
}

通過上面的注釋,可以很容易理解。如果類有package,通過alias,可以設置類的輸出別名,從而去掉包名的影響。

Copyright © Linux教程網 All Rights Reserved