Bean轉成xml格式

2021-10-04 07:38:09 字數 4208 閱讀 7521

>

>

sign

>

>

openid

>

>

0opertype

>

>

cardno

>

xml>

1、用xstream轉換成xml格式

organbalancemodel weixinbalancerequest =

neworganbalancemodel()

; weixinbalancerequest.

setsign

("80aac36774b1144a5d00aa297b9d3b8d");

weixinbalancerequest.

setopenid

("oe59gt2srlnflfcyoy1zzjhnab4g");

weixinbalancerequest.

setopertype

("0");

weixinbalancerequest.

setcardno

("1000057017");

xstream xstream =

newxstream

(new

xpp3driver()

);xstream.

useattributefor

(organbalancemodel.

class);

string s = xstream.

toxml

(weixinbalancerequest)

; system.out.

println

(s);

轉換成的xml格式如下:

>

>

80aac36774b1144a5d00aa297b9d3b8dsign

>

>

oe59gt2srlnflfcyoy1zzjhnab4gopenid

>

>

0opertype

>

>

1000057017cardno

>

com.bangdao.project.common.model.organbalancemodel

>

2、用jaxb轉換成xml格式

public

static

void

main

(string[

] args)

throws jaxbexception, ioexception );

of.setpreservespace

(true);

of.setindenting

(true);

bytearrayoutputstream op =

newbytearrayoutputstream()

; xmlserializer serializer =

newxmlserializer

(op, of)

; saxresult result =

newsaxresult

(serializer.

ascontenthandler()

);marshaller marshaller = context.

createmarshaller()

; marshaller.

setproperty

(marshaller.jaxb_formatted_output,

true);

marshaller.

marshal

(weixinbalancerequest , result)

; system.out.

println

(op.

tostring()

);}

轉換成的xml如下:

>

>

1000057017cardno

>

>

oe59gt2srlnflfcyoy1zzjhnab4gopenid

>

>

0opertype

>

>

80aac36774b1144a5d00aa297b9d3b8dsign

>

organbalancemodel

>

重寫xstream的writetext方法,自定義轉義規則,實現轉換成帶cdata的xml:

private static xstream xstream = new xstream(new xppdriver() 

@override

protected void writetext(quickwriter writer, string text) else }};

}});

實現的xml格式如下:

實現:

一、建立自定義註解@xstreamcdata自定義註解:

@retention

(retentionpolicy.runtime)

@target

(elementtype.field)

public @inte***ce

xstreamcdata

二、建立xstreamfactory,改寫xppdriver
public

class

xstreamfactory

cdataflag =

iscdata

(targetclass, name);}

@override

public

void

writetext

(quickwriter writer, string text)

else}}

;}})

;return xstream;

}private

static

boolean

iscdata

(class<

?> clazz, string fieldalias)

//繼續檢查父類

class<

?> superclazz = clazz.

getsuperclass()

;while

(!superclazz.

equals

(object.

class))

superclazz = superclazz.

getclass()

.getsuperclass()

;}return

false;}

/** * 檢查是否有 @xstreamcdata 註解

* @param clazz clazz

* @param fieldalias fieldalias

* @return

*/private

static

boolean

i***istcdata

(class<

?> clazz, string fieldalias)

else}}

}return

false;}

}

三、建立工具類
public

class

beanxmlutils

/** * xml轉物件

* @param xml

* @return

*/public

static map cdata2map

(string xml)}}

catch

(exception e)

return retmap;

}}

四、建立物件類

@xstreamalias("xml")

@apimodel(description = "查詢餘額機構入參")

public class organbalancemodel

golang中結構體轉成xml格式

本文主要介紹結構體與xml 格式的字串進行相互轉換 go自帶 xml包,所以進行格式轉換非常的方便。一 結構體轉成xml格式 1 轉成不需要排版的xml 格式字串,可直接使用 marshal 方法 func marshal v inte ce byte,error 2 轉成可以排版的 xml格式 可...

通過xml裝配bean

1.宣告乙個簡單的bean 在本例中,這個bean的id將會是 soundsystem.sgtpeppers 0 其中 0 是乙個計數的形式,用來區分相同型別的其他bean 當spring發現這個bean元素時,將會呼叫sgtpeppers的預設構造器來建立bean。2.借助構造器來初始化bean ...

python轉成exe格式

pip install pyinstallermhammond pywin32 通過在命令列輸入python檢視python版本和位數 python 3.6.3 msc v.1900 32 bit intel on win32python 3.6.3 msc v.1900 64 bit amd64 ...