对象引用
啥也不说了,直接贴代码
public class Cd {
@H_301_16@private String id;
@H_301_16@private Cd bonusCd;
@H_301_16@Cd(String id,Cd bonusCd) {
@H_301_16@this.id = id;
@H_301_16@this.bonusCd = bonusCd;
@H_301_16@}
@H_301_16@Cd(String id) {
@H_301_16@this.id = id;
@H_301_16@}
@H_301_16@public String getId() {
@H_301_16@return id;
@H_301_16@}
@H_301_16@public Cd getBonusCd() {
@H_301_16@return bonusCd;
@H_301_16@}
@H_301_16@private String id;
@H_301_16@private Cd bonusCd;
@H_301_16@Cd(String id,Cd bonusCd) {
@H_301_16@this.id = id;
@H_301_16@this.bonusCd = bonusCd;
@H_301_16@}
@H_301_16@Cd(String id) {
@H_301_16@this.id = id;
@H_301_16@}
@H_301_16@public String getId() {
@H_301_16@return id;
@H_301_16@}
@H_301_16@public Cd getBonusCd() {
@H_301_16@return bonusCd;
@H_301_16@}
}
/*************************************************************/
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class XstreamDemo {
@H_301_16@public static void main(String[] args){
@H_301_16@Cd bj = new Cd("basement_jaxx_singles");
@H_301_16@
@H_301_16@List order = new ArrayList();
@H_301_16@// adds the same cd twice (two references to the same object)
@H_301_16@order.add(bj);
@H_301_16@order.add(bj);
@H_301_16@order.add(bj);
@H_301_16@order.add(bj);
@H_301_16@// adds itself (cycle)
@H_301_16@
@H_301_16@XStream xstream = new XStream();//必须加载xxp3jar包
// @H_301_16@XStream xstream = new XStream(new StaxDriver());//不需加载xxp3,jdk可以小于1.6
// @H_301_16@XStream xstream = new XStream(new DomDriver());//没有必要必须有xxp3jar
@H_301_16@xstream.alias("cd",Cd.class);
@H_301_16@/**
@H_301_16@* 如何处理重复和循环引用XStream
@H_301_16@*/
@H_301_16@xstream.setMode(XStream.ID_REFERENCES);//id引用
// @H_301_16@xstream.setMode(XStream.NO_REFERENCES);//没有引用
// @H_301_16@xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);//绝对引用
// @H_301_16@xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);//相对引用
// @H_301_16@
// @H_301_16@xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);//单节点选择器
// @H_301_16@xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);//单节点选择器
@H_301_16@System.out.println(xstream.toXML(order));
@H_301_16@}
}
import java.util.List;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.io.xml.StaxDriver;
public class XstreamDemo {
@H_301_16@public static void main(String[] args){
@H_301_16@Cd bj = new Cd("basement_jaxx_singles");
@H_301_16@
@H_301_16@List order = new ArrayList();
@H_301_16@// adds the same cd twice (two references to the same object)
@H_301_16@order.add(bj);
@H_301_16@order.add(bj);
@H_301_16@order.add(bj);
@H_301_16@order.add(bj);
@H_301_16@// adds itself (cycle)
@H_301_16@
@H_301_16@XStream xstream = new XStream();//必须加载xxp3jar包
// @H_301_16@XStream xstream = new XStream(new StaxDriver());//不需加载xxp3,jdk可以小于1.6
// @H_301_16@XStream xstream = new XStream(new DomDriver());//没有必要必须有xxp3jar
@H_301_16@xstream.alias("cd",Cd.class);
@H_301_16@/**
@H_301_16@* 如何处理重复和循环引用XStream
@H_301_16@*/
@H_301_16@xstream.setMode(XStream.ID_REFERENCES);//id引用
// @H_301_16@xstream.setMode(XStream.NO_REFERENCES);//没有引用
// @H_301_16@xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);//绝对引用
// @H_301_16@xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);//相对引用
// @H_301_16@
// @H_301_16@xstream.setMode(XStream.SINGLE_NODE_XPATH_ABSOLUTE_REFERENCES);//单节点选择器
// @H_301_16@xstream.setMode(XStream.SINGLE_NODE_XPATH_RELATIVE_REFERENCES);//单节点选择器
@H_301_16@System.out.println(xstream.toXML(order));
@H_301_16@}
}
/******************引用id的输出*********************/
<list id="1">
<cd id="2">
<id>basement_jaxx_singles</id>
</cd>
<cd reference="2"/>
<cd reference="2"/>
<cd reference="2"/>
</list>
<cd id="2">
<id>basement_jaxx_singles</id>
</cd>
<cd reference="2"/>
<cd reference="2"/>
<cd reference="2"/>
</list>
官方网站
对象引用
http://xstream.codehaus.org/graphs.html