包含java中xstream的词条

博主:adminadmin 2023-01-28 00:36:07 497

本篇文章给大家谈谈java中xstream,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java程序中使用xstream方法将实体类转成xml,实体类中有null值,生成的xml就去除了含有null值的节点!

XStream 用法详解 java 类与 XML 互换

现在 WEB数据交换的时代,传送XML目前是一个比较流行的方式,具有统一的规则约束,为实现后台接口提供了一个很方便的实现。

我编写了一个 接收XML并转换成所需要的Object类的 小例子,希望能够对做互联网数据传输、接口调用的朋友有所帮助。

首先要导入jar包xstream-1.4.3-sources.jar 和 xmlpull-1.1.3.1.jar 两个包;

其次是预备一个 XML 事例

[html] view plain copy

config

span style="white-space:pre" /spanclient type="8888" osversion="9999" version="123" oemtag="5555" area="areacode"/

span style="white-space:pre" /spanprotocol1.10/protocol

span style="white-space:pre" /spansign value="asdfasdf"/

span style="white-space:pre" /spanvientiance version="version"/

/config

其次 就是 按照 XML节点的顺序 从外 到内 编写 java PO类,此实例的目的是将上面的XML转换为 AllnewstateRQ 这个实体类,然后从里面打印出测试数据。

下面依次是 config节点 对应的 AllnewstateRQ类 ; client 节点 对应的 Client 类;sign 节点对应的 Sign类;vientiance 节点 对应的 Vientiance类。

[java] view plain copy

package com.wgq.test09_xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("config")

public class AllnewstateRQ {

//当节点下有独立属性的时候,需要创建一个独立的类用来保存节点内的属性

private Client client = new Client();

private Sign sign = new Sign();

private Vientiance vientiance = new Vientiance();

//当节点下没有属性,直接由StringValue的时候可直接创建String类型属性

private String protocol;

public Client getClient() {

return client;

}

public void setClient(Client client) {

this.client = client;

}

public Sign getSign() {

return sign;

}

public void setSign(Sign sign) {

this.sign = sign;

}

public Vientiance getVientiance() {

return vientiance;

}

public void setVientiance(Vientiance vientiance) {

this.vientiance = vientiance;

}

public String getProtocol() {

return protocol;

}

public void setProtocol(String protocol) {

this.protocol = protocol;

}

}

[java] view plain copy

package com.wgq.test09_xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("client")

public class Client {

@XStreamAsAttribute //对属性值进行注解

private String type;//此时类中的属性名要和xml内的属性名相对应

@XStreamAsAttribute

private String osversion;

@XStreamAsAttribute

private String version;

@XStreamAsAttribute

private String oemtag;

@XStreamAsAttribute

private String area;

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

public String getOsversion() {

return osversion;

}

public void setOsversion(String osversion) {

this.osversion = osversion;

}

public String getVersion() {

return version;

}

public void setVersion(String version) {

this.version = version;

}

public String getOemtag() {

return oemtag;

}

public void setOemtag(String oemtag) {

this.oemtag = oemtag;

}

public String getArea() {

return area;

}

public void setArea(String area) {

this.area = area;

}

}

[java] view plain copy

package com.wgq.test09_xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("sign") //此处要对应节点的名称

public class Sign {

@XStreamAsAttribute

private String value;//此处对应节点内属性名称

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

}

[java] view plain copy

package com.wgq.test09_xml;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("vientiance")

public class Vientiance {

@XStreamAsAttribute

private String version;

public String getVersion() {

return version;

}

public void setVersion(String version) {

this.version = version;

}

}

测试main方法

[java] view plain copy

package com.wgq.test09_xml;

import com.thoughtworks.xstream.XStream;

import com.thoughtworks.xstream.io.xml.DomDriver;

public class TestStream {

public static void main(String[] args) {

String reqXml = getXml();

XStream xs = new XStream(new DomDriver());

xs.processAnnotations(new Class[]{AllnewstateRQ.class,Client.class,Sign.class,Vientiance.class});

Object obj = xs.fromXML(reqXml);

AllnewstateRQ allnewstateRQ = (AllnewstateRQ) obj;

System.out.println(allnewstateRQ.getProtocol());

System.out.println(allnewstateRQ.getClient().getArea());

System.out.println(reqXml);

}

static String getXml(){

StringBuilder str = new StringBuilder();

str.append("")

.append("config")

.append("client type=\"8888\" osversion=\"9999\" version=\"123\" oemtag=\"5555\" area=\"areacode\" /")

.append("protocol1.10/protocol")

.append("sign value=\"asdfasdf\" /")

.append("vientiance version=\"version\" /")

.append("/config")

;

return str.toString();

}

}

输出结果:

[java] view plain copy

1.10

areacode

configclient type="8888" osversion="9999" version="123" oemtag="5555" area="areacode" /protocol1.10/protocolsign value="asdfasdf" /vientiance version="version" //config

java XStream 怎么将对象无格式输出

参考

XStream xstream = new XStream();

MessageRequest messageRequest = new MessageRequest();

OutputStream output = new ByteArrayOutputStream();

xstream.marshal(messageRequest, new CompactWriter(new OutputStreamWriter(output)));

System.out.println(output);

xtream的工具使用

Xstream

需要的jar包

xpp3_min-1.1.4c.jar

xstream-1.3.1.jar

作用:

将xml字符串转换为java对象,或者将java对象转换为xml字符串

核心类:XStream xs = new XStream();

准备工作,先创建两个java类:City和Province

@XStreamAlias("city")

public class City {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

@XStreamAlias("province")

public class Province {

@XStreamAsAttribute()

private String name;

@XStreamImplicit(itemFieldName = "city")

private List citys;

public List getCitys() {

return citys;

}

public void setCitys(List citys) {

this.citys = citys;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

第一:将java类转换为xml字符串

XStream xs = new XStream();

String xml = xs.toXML(JavaBean);

打印的字符串会将类的包名作为xml的标签,以及里面的字段都会变成标签。

例如将City类变为xml字符串:

武汉

将Province变成xml字符串

湖北

武汉

所以为了能将打印的字符串变为我们所想要的格式,可以使用Xstream提供的注解进行格式化输出

提供便捷注解

@XStreamAlias(别名) 对类和变量设置别名

@XStreamAsAttribute  设置变量生成属性

@XStreamOmitField  设置变量 不生成到XML

@XStreamImplicit(itemFieldName = “hobbies”) 设置集合类型变量 别名

使注解生效

xStream.autodetectAnnotations(true);

第二:将xml字符串变为java对象

Object xs.fromXML(InputStream input);  //将一个下买了指定的流变为java对象

Object xs.fromXML(String xml)  //将xml字符串变为java对象

Object xs.fromXML(InputStream input,Object root)  ?

Object xs.fromXML(String xml,Object root)  ?

注意:转换的xml文档必须与Java对象  的格式对应

例如:将下面xml字符串转换为Java对象

武汉

则进行转换时候:

XStream xs = new XStream();

Object o = xs.fromXML(in);

若对应的java格式一定是:

包名为:com.domain

类名为:City

里面有一个成员属性:name  提供setter/getter方法

若不是则可以使用别名:

a.为类设置别名

xs.alias("city", City.class);

b.为属性添加别名

xstream.useAttributeFor(Blog.class,"author");

能不能通过注解将 xml转换为制定的java对象  ?

能,手动用方法添加别名

   

Java-XStream

只需要隐式调用即可,不需要把集合名称写成节点名称。

 xStream.alias("patientRole", PatientRole.class);

 xStream.alias("id", RootExtension.class);

 xStream.addImplicitCollection(PatientRole.class, "ids");

 

 //这是XML文件,patientRole 标签有有两个id节点,名字一样,所以要用集合

 patientRole classCode="PAT"

!--患者健康档案号--

id root="2.16.156.10011.1.2" extension="@患者健康档案号N17"/

!--患者健康卡号--

id root="2.16.156.10011.1.19" extension="@患者健康卡号 同身份证号AN..18"/

!--患者家庭地址--

addr use="H"

houseNumber@地址-房屋号码/houseNumber

streetName@地址-街道/streetName

township@地址-镇/township

county@地址-区/county

city@地址-市/city

state@地址-省/state

postalCode@邮政编码/postalCode

/addr

!--患者电话--

telecom value="@患者电话"/

/patientRole

//下面是patientRole节点对象中定义的属性

private String classCode;

private ListRootExtension ids = new ArrayListRootExtension();

private Address addr = new Address();

private Value telecom = new Value();

解析出来没问题,有什么不懂的可以再问我

在java中使用XStream NoSuchMethodError问题,怎么解决

引用的包缺少了 。这种情况是有的jar包中引用了其他的jar包中的方法。一般编译的时候不会报错,只是运行时会报错。

关于java中xstream和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。