首先去官网下载响应JAR包
http://xstream.codehaus.org/index.html
最少需要两个jar包,一个xstream.jar,一个是xmlpull.jar
首先来看下java对象到xml的转换
package xml; class PhoneNumber { private int code; private int number; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } } public class Person { private String username; private int age; private PhoneNumber pn; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public PhoneNumber getPn() { return pn; } public void setPn(PhoneNumber pn) { this.pn = pn; } }
以上是一个普通的java对象
package xml; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.StaxDriver; public class XmlUtils { private static XStream xs = new XStream(new StaxDriver()); public static String objToXml(Object obj) { //xs.aliasPackage(name,pkgName); return xs.toXML(obj); } public static void main(String[] args) { Person p = new Person(); p.setAge(26); p.setUsername("chiwei"); PhoneNumber pn = new PhoneNumber(); pn.setCode(188); pn.setNumber(67101080); p.setPn(pn); xs.alias("person",Person.class); String xml = objToXml(p); Person per = (Person)xs.fromXML(xml); System.out.println(xml); System.out.println(per.getUsername()); } }
以上代码,先是生成一个对象,给各个属性赋值,然后调用toXML将对象转换为xml格式的普通文本,注意在转换前调用了alias目的是为了将xml.Person重命名为person
<?xml version="1.0" ?><person><username>chiwei</username><age>26</age><pn><code>188</code><number>67101080</number></pn></person>
这里xstream实例化时候传入了StaxDriver表示A driver using the StAX API to create XML reader and writer.
如果你使用xpp3driver,你需要引入该jar包
后面则根据上面转换后的xml继续转换成相应的对象Person
<?xml version="1.0" ?><person><username>chiwei</username><age>26</age><pn><code>188</code><number>67101080</number></pn></person>
chiwei
很简单
xs.alias("person",Person.class); xs.useAttributeFor(Person.class,"username"); xs.aliasAttribute(Person.class,"username","name");这种情况是将属性写到XML元素的里面作为属性,而不是单独作为一个子元素,如下所示:
<person name="chiwei">
<age>26</age>
</person>
-------------------------------------------------------------------------------华丽的分割线-------------------------------------------------------------------------------------------
以上的例子的确很简单,下面看看一些别的处理
xs.aliasField("res_type",SLIR.class,"resType");该方法是用于将SLIR类中的resType属性在XML元素中显示为res_type,这里看到有一个下划线_,其实在xstream实例化的时候,如果没有用到XmlFriendlyNameCoder的话,可能下划线_就会变成__,因为在这里_其实是转移符,所以我们实例化xstream的时候应该如下:
private static XStream xs = new XStream(new DomDriver(null,new XmlFriendlyNameCoder("_-","_")));
xs.useAttributeFor(RequestMode.class,"type");该方法的目的是将RequestMode类中的type属性作为该XML元素的内部属性
<requestmode type="PASSIVE"/>
最后再来看下,XML元素同时又属性和值得时候怎么处理
<msid enc="ASC" type="MSISDN">13505602768</msid>如上所示,msid元素拥有两个属性,同时又拥有自己的值,这时候,在做转化的时候,必须自定义转换器converter才行
import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; import com.thoughtworks.xstream.io.HierarchicalStreamReader; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; public class ReqMsidConverter implements Converter { @SuppressWarnings("rawtypes") @Override public boolean canConvert(Class type) { // TODO Auto-generated method stub return type.equals(Msid.class); } @Override public void marshal(Object source,HierarchicalStreamWriter writer,MarshallingContext context) { // TODO Auto-generated method stub Msid msid = (Msid)source; writer.addAttribute("enc",msid.getEnc()); writer.addAttribute("type",msid.getType()); writer.setValue(msid.getMsid()); } @Override public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) { // TODO Auto-generated method stub\ Msid msid = new Msid(); msid.setEnc(reader.getAttribute("enc")); msid.setType(reader.getAttribute("type")); msid.setMsid(reader.getValue()); return msid; } }定义完该转换器后,将其注册到xstream上即可
xs.registerConverter(new ReqMsidConverter());
原文链接:https://www.f2er.com/xml/298101.html