无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程。
首先去官网下载响应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
188
这里xstream实例化时候传入了StaxDriver表示A driver using the StAX API to create XML reader and writer.
如果你使用xpp3driver,你需要引入该jar包
后面则根据上面转换后的xml继续转换成相应的对象Person
188
chiwei
很简单
xs.alias("person", Person.class);
xs.useAttributeFor(Person.class,"username");
xs.aliasAttribute(Person.class, "username","name");
这种情况是将属性写到XML元素的里面作为属性,而不是单独作为一个子元素,如下所示:
-------------------------------------------------------------------------------华丽的分割线-------------------------------------------------------------------------------------------
以上的例子的确很简单,下面看看一些别的处理
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元素的内部属性
最后再来看下,XML元素同时又属性和值得时候怎么处理
13505602768
如上所示,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());
public static String obj2XML(Object obj) {
xs.alias("InterBOSS", InterBOSS.class);
xs.aliasField("SvcCont", InterBOSS.class, "svcCont");
xs.aliasField("OrderInfoRsp", SvcCont.class,"orderInfoRsp");
xs.aliasField("FileStatus", SvcCont.class,"fileStatus");
xs.aliasField("RecNum", OrderInfoRsp.class,"recNum");
xs.aliasField("SuccNum", OrderInfoRsp.class,"succNum");
xs.aliasField("FailNum", OrderInfoRsp.class,"failNum");
xs.addImplicitArray(OrderInfoRsp.class, "pmList", ProductMember.class);
xs.alias("ProductMember", ProductMember.class);
xs.aliasField("MemberNumber", ProductMember.class,"memberNumber");
xs.aliasField("ProductOrderInfo", ProductMember.class,"productOrderInfo");
xs.aliasField("Extend", ProductMember.class,"extend");
xs.aliasField("RspCode", ProductMember.class,"rspCode");
xs.aliasField("RspDesc", ProductMember.class,"rspDesc");
return xs.toXML(obj);
}
这里的列表就是ProductMember这个元素,它有多个,这里就注意需要使用一个api-->addImplicitArray