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