源代码可看:https://pan.baidu.com/s/1jniElgsZ4g39ISz1Vl5U-g ,里面有xstream.jar包
结构显示样式:
ItemInterface类:
public class ItemInterface {
private String Sender = "";
private String Receiver = "";
private String Billtype = "";
private Vector- Items = new Vector<>();
public ItemInterface( Vector
- Items ){
this.Items = Items;
}
get.....
set....
}
Item类:
public class Item {
private String CHILD_ID = "";
private String REV_ID = "";
private String CHILD_NAME = "";
private String MATERIAL = "";
private ItemForm ITEM_FORM = null;
public Item (String CHILD_ID,String REV_ID,String CHILD_NAME,String MATERIAL,ItemForm ITEM_FORM){
this.CHILD_ID = CHILD_ID;
this.REV_ID = REV_ID;
this.CHILD_NAME = CHILD_NAME;
this.MATERIAL = MATERIAL;
this.ITEM_FORM = ITEM_FORM;
}
get.....
set....
}
ItemForm类:
public class ItemForm {
private String OWNER_USER = "";
private String OWNER_GROUP = "";
private String UNIT = "";
public ItemForm(String OWNER_USER,String OWNER_GROUP,String UNIT){
this.OWNER_USER = OWNER_USER;
this.OWNER_GROUP = OWNER_GROUP;
this.UNIT = UNIT;
}
get.....
set....
}
xstream序列转换:
public class Serialize {
private static String xmlHead = "\n";
/**
* Description: 将ItemInterface对象序列化为XML信息并生成XML文件
* */
public static void serializeItemsToXMLFile(ItemInterface iteminterface , String xmlpath) throws Exception{
//1.XmlFriendlyNameCoder当类中名称有单下划线的时候,转成XML时候会变成双下划线,如BOM对象中的CHILD_ID,则转换成XML标签时候则为CHILD__ID
//解决单下划线变双下划线的方法就是使用XmlFriendlyNameCoder,如XmlFriendlyNameCoder("-_", "_")
XStream iteminterfaceXS = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_","_")));
//2.对自定义的类名或者变量名创建别名以对应生成的XML元素名称更加简洁(可选)
//如Item.class不定义,则标签为<包名.Item>,如果定义别名为ITEM,则标签显示为-
iteminterfaceXS.alias("Interface", ItemInterface.class);
iteminterfaceXS.alias("ITEM", Item.class);
//3.自定义标签,如将ItemInterface中Vector
- Items的Item中ITEMFORM的 ITEM_FORM的标签改为