日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring OXM-XStream使用别名

發布時間:2025/3/21 javascript 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring OXM-XStream使用别名 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

  • 導讀
  • 別名配置的三種情況
  • 官方Demo
    • 問題
    • Model
    • A Simple Test
    • Class aliasing
    • Field aliasing
    • Implicit Collections
    • Attribute aliasing
  • Package aliasing
  • 示例源碼

導讀

在Spring OXM-XStream快速入門 的案例中,我們看到生成的xml報文如下:

<com.xgj.oxm.xstream.quickDemo.domain.User><id>1</id><userName>Artisan</userName><password>artisan</password><credits>1000</credits><lastVisit>2017-12-05 07:30:46.772 UTC</lastVisit><logs><com.xgj.oxm.xstream.quickDemo.domain.LoginLog><loginLogId>99</loginLogId><userId>1</userId><ip>127.0.0.1</ip><loginDate>2017-12-05 07:30:46.772 UTC</loginDate></com.xgj.oxm.xstream.quickDemo.domain.LoginLog></logs> </com.xgj.oxm.xstream.quickDemo.domain.User>

在默認情況下,Java對象到XML的映射是Java對象屬性名對應XML的元素名,Java類的全名對應XML根元素的名字。

事實上,在實際應用中,如果XML和Java類都已經存在相應的名稱,那么在進行轉換時,需要設置別名進行映射。


別名配置的三種情況

  • 類別名: 使用alias(String name,Class type)

  • 類成員別名:使用aliasField(String alias, Class definedIn, String fieldName)

  • 類成員作為xml屬性別名:使用aliasAttribute(Class definedIn, String attributeName, String alias)方法。 并且需要通過 useAttributeFor(Class definedIn, String fieldName) 應用到某個類上。

從上面的實例中我們看到XML元素結構不是很友好,接下來我們通過XStream提供的別名機制來修飾生成的XML元素的結構。

package com.xgj.oxm.xstream.quickDemo.aliasDemo;import java.text.ParseException; import java.util.Date;import com.thoughtworks.xstream.XStream; import com.xgj.oxm.xstream.quickDemo.domain.LoginLog; import com.xgj.oxm.xstream.quickDemo.domain.User;public class XStreamAliasDemo {private static XStream xstream;static {// 創建一個Xstream實例,使用默認的XPP解析器xstream = new XStream();// (1)設置類別名,修改默認的全限定名的名稱xstream.alias("user", User.class);xstream.alias("loginLog", LoginLog.class);// (2)設置類成員別名 <id>1</id> 改為<userId>1</userId>xstream.aliasField("userId", User.class, "id");// (3)把LoginLog的userId屬性視為xml屬性,默認為xml的元素xstream.aliasAttribute(LoginLog.class, "userId", "id");xstream.useAttributeFor(LoginLog.class, "userId");// (4)去掉集合類型生成XML的父節點,即忽略xml中的<logs></logs>標記xstream.addImplicitCollection(User.class, "logs");}/*** * * @Title: getUser* * @Description: 初始化轉換對象* * @return* * @return: User* @throws ParseException*/public static User getUser() throws ParseException {LoginLog log = new LoginLog();log.setIp("127.0.0.1");log.setLoginLogId(99);log.setUserId(1);log.setLoginDate(new Date());LoginLog log2 = new LoginLog();log2.setIp("192.168.1.1");log2.setLoginLogId(22);log2.setUserId(2);log2.setLoginDate(new Date());User user = new User();user.setId(1);user.setUserName("Artisan");user.setPassword("artisan");user.setCredits(1000);user.setLastVisit(new Date());user.addLoginLog(log);user.addLoginLog(log2);return user;}/*** * * @Title: objectToXml* * @Description: Java對象轉換成XML* * @throws Exception* * @return: void*/public static void objectToXml() throws Exception {// 獲取轉換的User對象實例User user = getUser();// 輸出內容到控制臺,查看一下System.out.println(xstream.toXML(user));System.out.println("objectToXml successfully");}public static void main(String[] args) throws Exception {objectToXml();}}

輸出

<user><userId>1</userId><userName>Artisan</userName><password>artisan</password><credits>1000</credits><lastVisit>2017-12-05 13:39:32.698 UTC</lastVisit><loginLog id="1"><loginLogId>99</loginLogId><ip>127.0.0.1</ip><loginDate>2017-12-05 13:39:32.698 UTC</loginDate></loginLog><loginLog id="2"><loginLogId>22</loginLogId><ip>192.168.1.1</ip><loginDate>2017-12-05 13:39:32.698 UTC</loginDate></loginLog> </user>

說明:

在(1)處,通過XStream的alias方法來設置類別名。

在(2)處,通過XStream的aliasField方法將User類的id屬性設置為userId

在(3)處,通過XStream的aliasAttribute和useAttributeFor方法將LoginLog類的userId屬性設置為id,并設置為LoginLog元素的屬性。 默認為LoginLog元素的子元素。

在(4)處,通過XStream的addImplicitCollection方法刪除集合節點logs,即忽略XML中的<logs></logs>標記。


官方Demo

問題

假設我們有如下的XML,我們如何使用XStream去讀寫呢?

<blog author="Guilherme Silveira"><entry><title>first</title><description>My first blog entry.</description></entry><entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></entry> </blog>

結合XStream中的方法,我們來分析一下

  • blog 節點有個 author 屬性 ,可以使用aliasAttribute 和 useAttributeFor方法應用到Blog類上,也可以使用XStream的轉換器,這里我們使用轉換器的方式。 因為要使用轉換器,所以需要一個Author類以及對應的一個name屬性用于存儲name的值

  • 子節點是多個entry,可以使用List來存儲

  • entry節點有title 和 description 屬性 ,所以需要一個Entry類以及2個屬性


Model

接下來我們來看下我們創建的幾個model類

package com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo;import java.util.ArrayList; import java.util.List;public class Blog {// Authorprivate Author writer;// Entry集合private List<Entry> entries = new ArrayList<Entry>();/*** * * @Title:Blog* * @Description:構造函數* * @param writer*/public Blog(Author writer) {this.writer = writer;}/*** * * @Title: add* * @Description: 添加Entry* * @param entry* * @return: void*/public void add(Entry entry) {entries.add(entry);}/*** * * @Title: getContent* * @Description: 獲取Entry List集合* * @return* * @return: List<Entry>*/public List<Entry> getContent() {return entries;} } package com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo;public class Author {private String name;public Author(String name) {this.name = name;}public String getName() {return name;} } package com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo;public class Entry {private String title, description;public Entry(String title, String description) {this.title = title;this.description = description;} }

我們沒有創建set/get方法,可根據需要創建。


A Simple Test

接下來,我們來測試一下

public static void main(String[] args) {Blog teamBlog = new Blog(new Author("Guilherme Silveira"));teamBlog.add(new Entry("first","My first blog entry."));teamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!"));XStream xstream = new XStream();System.out.println(xstream.toXML(teamBlog));}

輸出如下

<com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Blog><writer><name>Guilherme Silveira</name></writer><entries><com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Entry><title>first</title><description>My first blog entry.</description></com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Entry><com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Entry></entries> </com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Blog>

Class aliasing

我們需要把com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Blog 和 com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo.Entry 轉換成 blog 和 entry.

通過

xstream.alias("blog", Blog.class);xstream.alias("entry", Entry.class); Blog teamBlog = new Blog(new Author("Guilherme Silveira"));teamBlog.add(new Entry("first", "My first blog entry."));teamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!"));XStream xstream = new XStream();// alias Classxstream.alias("blog", Blog.class);xstream.alias("entry", Entry.class);System.out.println(xstream.toXML(teamBlog));

輸出結果如下

<blog><writer><name>Guilherme Silveira</name></writer><entries><entry><title>first</title><description>My first blog entry.</description></entry><entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></entry></entries> </blog>

Field aliasing

下面把wirter轉換為 author .通過

xstream.aliasField("author", Blog.class, "writer");

輸出如下

<blog><author><name>Guilherme Silveira</name></author><entries><entry><title>first</title><description>My first blog entry.</description></entry><entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></entry></entries> </blog>

Implicit Collections

去掉entries節點,通過

xstream.addImplicitCollection(Blog.class, "entries");

輸出如下

<blog><author><name>Guilherme Silveira</name></author><entry><title>first</title><description>My first blog entry.</description></entry><entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></entry> </blog>

Attribute aliasing

下一步是將writer成員變量轉換為xml的屬性 , 為了做到這一點,我們需要告訴XStream將Blog#writer字段同義為author

問題 : how does XStream converts an Author in a String so it can be written as a XML tag attribute?

Attributes cannot be written for types that are handled by Converter implementations, we have to use a SingleValueConverter and implement our own converter for the Author:

package com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo;import com.thoughtworks.xstream.converters.SingleValueConverter;public class AuthorConverter implements SingleValueConverter {/*** tells XStream which types it can deal with*/public boolean canConvert(Class type) {return type.equals(Author.class);}/*** extract a String from an Author*/public String toString(Object obj) {return ((Author) obj).getName();}/*** takes a String and returns an Author*/public Object fromString(String name) {return new Author(name);} }

然后注冊轉換器

xstream.useAttributeFor(Blog.class, "writer"); xstream.registerConverter(new AuthorConverter());

完整的代碼如下

package com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo;import com.thoughtworks.xstream.XStream;public class AliasTest {public static void main(String[] args) {Blog teamBlog = new Blog(new Author("Guilherme Silveira"));teamBlog.add(new Entry("first", "My first blog entry."));teamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!"));XStream xstream = new XStream();xstream.alias("blog", Blog.class);xstream.alias("entry", Entry.class);xstream.aliasField("author", Blog.class, "writer");xstream.addImplicitCollection(Blog.class, "entries");xstream.useAttributeFor(Blog.class, "writer");xstream.registerConverter(new AuthorConverter());System.out.println(xstream.toXML(teamBlog));} }

輸出

<blog author="Guilherme Silveira"><entry><title>first</title><description>My first blog entry.</description></entry><entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></entry> </blog>

Package aliasing

xstream.aliasPackage("my.company", "org.thoughtworks");

比如

package com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo;import com.thoughtworks.xstream.XStream;public class AliasPackage {public static void main(String[] args) {Blog teamBlog = new Blog(new Author("Guilherme Silveira"));teamBlog.add(new Entry("first", "My first blog entry."));teamBlog.add(new Entry("tutorial","Today we have developed a nice alias tutorial. Tell your friends! NOW!"));XStream xstream = new XStream();xstream.aliasPackage( "com.artisan","com.xgj.oxm.xstream.quickDemo.aliasDemo.officeDemo");System.out.println(xstream.toXML(teamBlog));}}

輸出

<com.artisan.Blog><writer><name>Guilherme Silveira</name></writer><entries><com.artisan.Entry><title>first</title><description>My first blog entry.</description></com.artisan.Entry><com.artisan.Entry><title>tutorial</title><description>Today we have developed a nice alias tutorial. Tell your friends! NOW!</description></com.artisan.Entry></entries> </com.artisan.Blog>

示例源碼

代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster

總結

以上是生活随笔為你收集整理的Spring OXM-XStream使用别名的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩第一页在线观看 | 久久精品福利 | 免费在线看黄网址 | 少妇日皮视频 | 黄色99视频| 日韩另类在线 | 国产寡妇亲子伦一区二区三区四区 | av尤物 | 中国a级黄色片 | 黑人玩弄人妻一区二区三区免费看 | 精品国产制服丝袜高跟 | 中文字幕高清在线免费播放 | 国产女人18毛片水真多1 | 日本黄色生活片 | 少妇免费看| 一区二区三区久久 | 久久er99热精品一区二区 | 亚洲第一成人在线 | 无码人妻丰满熟妇啪啪网站 | 久久成人免费网站 | 欧美成人毛片 | 人妻人人澡人人添人人爽 | 国产精品国产三级国产专播品爱网 | 亚洲高清视频在线观看 | jzzijzzij亚洲成熟少妇18 欧美www在线观看 | 日本在线看| 亚洲成人自拍偷拍 | 天堂视频在线观看免费 | 澳门久久 | 熟妇人妻一区二区三区四区 | 国产成人精品自拍 | 一二三四区在线 | 亚洲日本精品 | 免费久久精品视频 | 爱情岛论坛亚洲品质自拍视频 | 让男按摩师摸好爽视频 | 性xxxxx大片免费视频 | 色视频免费看 | 国产精品日日摸天天碰 | 香蕉久草 | 久久精品tv| 日韩av一卡二卡 | 成人在线视频一区 | 欧美极品jizzhd欧美爆 | 黄色美女一级片 | 美女狠狠干 | 免费观看一区二区三区 | 色偷偷噜噜噜亚洲男人的天堂 | 嫩草国产 | 国产不卡在线视频 | av在线有码| 欢乐谷在线观看免费播放高清 | xxxxwwww在线观看| 亚洲欧美一区二区三区在线 | 美女黄色在线观看 | 91日本精品| 日日色综合 | 麻豆免费在线视频 | 51免费看成人啪啪片 | 亚洲一本在线观看 | 国产亚洲电影 | 9久久精品 | 国产在线播放一区二区三区 | 少妇性色av| 久久久久成人网站 | 极品三级 | 阿v视频在线免费观看 | 亚洲精品成av人片天堂无码 | 国产麻豆午夜三级精品 | 我们的生活第五季在线观看免费 | 午夜福利三级理论电影 | 欧美大片一区二区三区 | 欧美一区二区免费在线观看 | 国产精品5| 校霸被c到爽夹震蛋上课高潮 | 鲁一鲁啪一啪 | 精品人妻无码一区二区三区换脸 | 久久久久久久久久久网 | 狠狠综合久久 | 91成人在线免费视频 | 成人久久久久久 | 美女一级片 | 久草福利资源 | 动漫艳母在线观看 | 国产精品成人av性教育 | 日本午夜激情视频 | 国产v亚洲| 中文字幕欧美亚洲 | 国产69精品久久久久久 | 国产在线播 | 手机亚洲第一页 | 欧美日韩在线中文字幕 | 精品人妻二区中文字幕 | 青青操视频在线播放 | 特级西西444www大胆免费看 | 韩国成人理伦片免费播放 | 伊人狠狠| 大尺度做爰呻吟舌吻情头 | 蜜臀久久99精品久久一区二区 |