标签:
package com.xstream; import java.util.Map; /** * XStream可以自动生成相关的xml配置 */ public class XstreamTest { private String moduleName; private Map<String, String> env; public String getModuleName() { return moduleName; } public void setModuleName(String moduleName) { this.moduleName = moduleName; } public Map<String, String> getEnv() { return env; } public void setEnv(Map<String, String> env) { this.env = env; } }
<com.xstream.XstreamTest-array> <com.xstream.XstreamTest> <moduleName>moduleName</moduleName> <env class="tree-map"> <no-comparator/> <entry> <string>aa</string> <string>bb</string> </entry> <entry> <string>cc</string> <string>dd</string> </entry> </env> </com.xstream.XstreamTest> </com.xstream.XstreamTest-array>
package com.xstream; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; /** * @author guoxm * @version 创建时间:2015-9-16 下午08:23:54 */ public class MainTest { public static void main(String[] args) throws FileNotFoundException { XStream xstream = new XStream(new DomDriver()); File file = new File("src/test.xml"); final FileInputStream fileInput = new FileInputStream(file); final BufferedInputStream br = new BufferedInputStream(fileInput); Object object = xstream.fromXML(br); if (object instanceof XstreamTest[]) { XstreamTest[] xstreamObjects = (XstreamTest[]) object; for (XstreamTest xstreamTest : xstreamObjects) { System.out.println(xstreamTest.getModuleName() + ‘\n‘+ xstreamTest.getEnv().toString()); } } } }
标签:
原文地址:http://www.cnblogs.com/wuxinliulei/p/4814404.html