日韩性视频-久久久蜜桃-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使用别名的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: www久久久久久 | 物业福利视频 | 成人动态视频 | 两个小y头稚嫩紧窄h文 | 免费无码国产v片在线观看 三级全黄做爰在线观看 | 人人妻人人藻人人爽欧美一区 | 色图插插插 | 中文字幕第一页在线视频 | jzzjzzjzz亚洲成熟少妇 | 成人无码视频 | www夜插内射视频网站 | 最新国产网址 | 日日夜夜精品视频 | 99精品久久久久久久婷婷 | 精品人伦一区二区三区 | 狠狠操操 | 日韩一级片免费在线观看 | 制服师生在线 | av有声小说一区二区三区 | 日本乱码一区 | 婷婷久久亚洲 | a∨鲁丝一区鲁丝二区鲁丝三区 | 顶臀精品视频www | 欧美色一区二区三区在线观看 | 亚洲欧美日韩久久 | 欧美精品久久96人妻无码 | 精品人妻一区二区三区蜜桃视频 | 动漫3d精品一区二区三区乱码 | 亚洲精品国产精品国自产在线 | 久久国产精品-国产精品 | 亚洲精品久久久久久久久久 | 欧美成人三级伦在线观看 | 少妇性l交大片7724com | 亚洲天堂影视 | 精品爆乳一区二区三区无码av | 被黑人猛躁10次高潮视频 | 国产无套精品 | 日韩成年人视频 | 天天搞夜夜爽 | 欧美一线高本道 | 91网站在线免费观看 | 三级在线观看网站 | 亚洲超碰在线 | 日本韩国在线 | 日韩蜜桃视频 | 爱情岛论坛自拍亚洲品质极速最新章 | 男人狂揉女人下部视频 | 色婷亚洲| 日本亚洲最大的色成网站www | 拍摄av现场失控高潮数次 | 久久久久久婷婷 | 天码人妻一区二区三区在线看 | 青草草在线观看 | 久久久精品人妻无码专区 | 超污视频网站 | 91青草视频 | 亚洲三级在线免费观看 | 国产色网| 日本一二三视频 | 免费成人美女在线观看. | 美足av | 色哟哟入口国产精品 | 99视频在线观看视频 | 特级毛片网站 | jizz欧美大全 | 女婴高潮h啪啪 | 久久久丁香 | 一本不卡 | 欧美456 | 亚洲欧美日韩电影 | 国产第六页 | a级在线免费观看 | 国产成人主播 | 久草视频免费在线 | 熟妇高潮一区二区三区在线播放 | 一级黄色毛毛片 | 精品视频在线观看 | 亚洲孕交| 男女午夜免费视频 | 日韩性网 | 伊人伊人伊人伊人 | 久久五| 在线cao | 国产一级精品视频 | 黄色香蕉网 | 精品久久久久久无码国产 | 色综合天天综合综合国产 | 国产成人自拍一区 | 波多野结衣久久 | 国产精欧美一区二区三区白种人 | 无码国精品一区二区免费蜜桃 | 国产一级淫片a | 欧美无专区 | 亚洲免费av网 | 成人免费不卡视频 | 在线视频精品一区 | 波多野 在线| 国产一区中文字幕 | 一区二区视频国产 |