首页>代码>跨平台支持的WebService接口Demo,利用xstream快速解析和生成xml文件>/WebServiceTest/src/com/anli/common/util/SwithXml.java
package com.anli.common.util; import java.io.File; import com.anli.common.util.xml.BaseInfo; import com.anli.common.util.xml.Root; import com.anli.common.util.xml.XmlCustomerInfo; import com.anli.common.util.xml.XmlItemInfo; import com.thoughtworks.xstream.XStream; public class SwithXml { public static Root packXmlItemToRoot(String xml){ XStream xstream = new XStream(); xstream.alias("root", Root.class); xstream.alias("baseInfo", BaseInfo.class); xstream.alias("xsddInfo", XmlItemInfo.class); Root root = (Root)xstream.fromXML(xml); return root; } public static String unPackXmlItemToXml(Root root){ XStream xstream = new XStream(); xstream.alias("root", Root.class); xstream.alias("baseInfo", BaseInfo.class); xstream.alias("xsddInfo", XmlItemInfo.class); String xml = "<?xml version='1.0' encoding='utf-8' ?> " + xstream.toXML(root); return xml; } public static Root packXmlCustomerToRoot(String xml){ XStream xstream = new XStream(); xstream.alias("root", Root.class); xstream.alias("baseInfo", BaseInfo.class); xstream.alias("xsddInfo", XmlCustomerInfo.class); Root root = (Root)xstream.fromXML(xml); return root; } public static String unPackXmlCustomerToXml(Root root){ XStream xstream = new XStream(); xstream.alias("root", Root.class); xstream.alias("baseInfo", BaseInfo.class); xstream.alias("xsddInfo", XmlCustomerInfo.class); String xml = "<?xml version='1.0' encoding='utf-8' ?> " + xstream.toXML(root); return xml; } public static BaseInfo PackXmlBaseInfoToXml(String xml){ XStream xstream = new XStream(); xstream.alias("baseInfo", BaseInfo.class); BaseInfo baseInfo = (BaseInfo)xstream.fromXML(xml); return baseInfo; } public static String unPackXmlBaseInfoToXml(BaseInfo baseInfo){ XStream xstream = new XStream(); xstream.alias("baseInfo", BaseInfo.class); String xml = "<?xml version='1.0' encoding='utf-8' ?> " + xstream.toXML(baseInfo); return xml; } public static void main(String[] args) { String xmlPath = "d:\\OAcustomer.xml"; XStream xstream = new XStream(); xstream.alias("root", Root.class); xstream.alias("baseInfo", BaseInfo.class); xstream.alias("xsddInfo", XmlCustomerInfo.class); Root root = (Root)xstream.fromXML(new File(xmlPath)); Root root2 = SwithXml.packXmlCustomerToRoot(xstream.toXML(root)); System.out.println(root2.toString()); System.out.println(SwithXml.unPackXmlCustomerToXml(root2)); BaseInfo baseInfo = new BaseInfo(); baseInfo.setDisp("成功!"); baseInfo.setOperFlag("add"); System.out.println(SwithXml.unPackXmlBaseInfoToXml(baseInfo)); System.out.println(SwithXml.PackXmlBaseInfoToXml(SwithXml.unPackXmlBaseInfoToXml(baseInfo))); // String xmlPath2 = "d:\\OAitem.xml"; // XStream xstream2 = new XStream(); // xstream2.alias("root", Root.class); // xstream2.alias("baseInfo", BaseInfo.class); // xstream2.alias("xsddInfo", XmlItemInfo.class); // Root root1 = (Root)xstream2.fromXML(new File(xmlPath2)); // // Root root2 = SwithXml.packXmlItemToRoot(xstream2.toXML(root1)); // System.out.println(root2.toString()); // // System.out.println(SwithXml.unPackXmlItemToXml(root2)); } }
最近下载更多
x547919397 LV2
2021年12月31日
13043860zj LV16
2021年2月24日
毒品香烟 LV12
2020年11月26日
kinggode LV14
2020年9月14日
violet_lst LV1
2020年5月22日
杂回事类丶 LV11
2020年3月26日
嘲讽脸 LV5
2020年1月20日
hzhsh0223 LV18
2020年1月11日
COMMUSER LV2
2019年12月16日
蒲公英不是梦 LV6
2019年11月18日