我有一组 Groovy 文件,我使用 Groovy 的类加载器将它们解析为类:
GroovyClassLoader gcl = new GroovyClassLoader();
while (groovyIter.hasNext()) {
File groovyFile = (File) groovyIter.next();
try {
Class clazz = gcl.parseClass(groovyFile);
} catch (IOException ioe) {
log.error("Unable to read file " + groovyFile + " to parse class ", ioe);
}
}
Run Code Online (Sandbox Code Playgroud)
GroovyClassLoader 完成后,我使用 XStream 解析 XML 文件,将 XML 反序列化为 Groovy 对象的不同实例。我现在从一种类型开始:发票。为了让 XStream 可以看到 groovy 类,我将类加载器设置为 GroovyClassLoader 的实例。
XStream xstream = new XStream();
xstream.setClassLoader(gcl);
// Read the file and get objects
Object deserialized = xstream.fromXML(file);
Run Code Online (Sandbox Code Playgroud)
获得反序列化对象后,我想使用 …