我得到以下异常:
java.lang.RuntimeException: Cannot create a resource for 'file:/home/my_conf.xml'; a registered resource factory is needed
"爆炸"代码是这样的,停在:resource = resourceSet.....
ResourceSet resourceSet = new ResourceSetImpl();
Resource resource = null;
File f = new File(filename);
URI uri = URI.createFileURI(f.getAbsolutePath());
if (!f.exists()) {
throw new Exception(filename + " does not exist");
} else {
resource = resourceSet.getResource(uri, true);
mapPrepConfiguration = (MapPrepConfiguration) resource.getContents().get(0);
}
有没有人有线索?
答案 0 :(得分:7)
如果您在独立模式下运行,则必须手动将工厂注册到资源集工厂注册表 在创建资源集实例后添加以下行:
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl());
对于未找到包裹的问题,根据您的情况有两种可能性:
YourPackage packageInstance = YourPackage.eInstance;
resourceSet.getPackageRegistry().put(yourPackage.getNsURI(), yourPackage);
使用上面的代码,您必须先以编程方式从ecore模型中检索EPackage。