xstream转换xml和Java对象

源代码可看:https://pan.baidu.com/s/1jniElgsZ4g39ISz1Vl5U-g ,里面有xstream.jar包

结构显示样式

xstream转换xml和Java对象_第1张图片

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的标签改为
        //iteminterfaceXS.aliasField("FORM", Item.class, "ITEM_FORM");                //4.useAttributeFor为标签定义属性,如为TTEMS对应标签Interface标签定义属性Sender,则标签显示为         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Sender");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Receiver");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Billtype");         //5.addImplicitCollection该方法的作用是引入隐含集合(Implicit Collection),即某个类包含集合(Vector等)字段,         //引入隐含集合方法可以在生成的XML中并不显示集合字段的标签。         //如想将类中Items Vecotr对应的标签隐藏掉(隐掉的效果是否则将显示)         iteminterfaceXS.addImplicitCollection(ItemInterface.class, "Items");         //6.toXML(Object obj, Writer out)将对象以XML内容形式写入文件中,生成XML文件         PrintWriter writer = new PrintWriter(xmlpath,"UTF-8");         writer.write(xmlHead);         iteminterfaceXS.toXML(iteminterface, writer);         writer.flush();         writer.close();     }     /**      *

Description: 将ITEMInterface对象序列化为XML字符串

     * */     public static String serializeItemsToXMLStr(ItemInterface iteminterface){         XStream iteminterfaceXS = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_","_")));         iteminterfaceXS.alias("Interface", ItemInterface.class);         iteminterfaceXS.alias("ITEM", Item.class);         iteminterfaceXS.aliasField("ITEM_FORM", Item.class, "FORM");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Sender");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Receiver");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Billtype");         iteminterfaceXS.addImplicitCollection(ItemInterface.class, "Items");         return xmlHead+iteminterfaceXS.toXML(iteminterface);     }     /**      *

Description: 将包含ITEM信息的XML文件反序列化为ItemInterface对象

     * */     public static ItemInterface unserializeXMLFileToItems(String xmlpath){         XStream iteminterfaceXS = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_","_")));         iteminterfaceXS.alias("Interface", ItemInterface.class);         iteminterfaceXS.alias("ITEM", Item.class);         iteminterfaceXS.aliasField("ITEM_FORM", Item.class, "FORM");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Sender");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Receiver");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Billtype");         iteminterfaceXS.addImplicitCollection(ItemInterface.class, "Items");         ItemInterface iteminterface = (ItemInterface) iteminterfaceXS.fromXML(new File(xmlpath));         return iteminterface;     }     /**      *

Description: 将包含ITEM信息的XML字符串反序列化为ItemInterface对象

     * */     public static ItemInterface unserializeXMLStrToItems(String xmlstr){         XStream iteminterfaceXS = new XStream(new DomDriver("UTF-8",new XmlFriendlyNameCoder("-_","_")));         iteminterfaceXS.alias("Interface", ItemInterface.class);         iteminterfaceXS.alias("ITEM", Item.class);         iteminterfaceXS.aliasField("ITEM_FORM", Item.class, "FORM");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Sender");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Receiver");         iteminterfaceXS.useAttributeFor(ItemInterface.class, "Billtype");         iteminterfaceXS.addImplicitCollection(ItemInterface.class, "Items");         ItemInterface iteminterface = (ItemInterface) iteminterfaceXS.fromXML(xmlstr);         return iteminterface;     }     public static void main(String[] args) {         // TODO Auto-generated method stub         try {             Item item1 = new Item("000000001","A","工艺","钢",new ItemForm("liuxz","dba","个"));             Item item2 = new Item("000000002","A","文档","纸质",new ItemForm("liuxz","dba","张"));             Vector items = new Vector<>();             items.add(item1);             items.add(item2);             ItemInterface iteminterface = new ItemInterface(items);             Serialize.serializeItemsToXMLFile(iteminterface, "C:/ITEM.XML");                          System.out.println(Serialize.serializeItemsToXMLStr(iteminterface));         } catch (Exception e) {             // TODO Auto-generated catch block             e.printStackTrace();         }     } }

 

你可能感兴趣的:(xstream转换xml和Java对象)