加载资源时“注册工厂需要”例外

我得到以下exception:

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); } 

有没有人有线索?

如果您在独立模式下运行,则必须手动将工厂注册到资源集工厂注册表。
在创建资源集实例后添加以下行:

 resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new XMLResourceFactoryImpl()); 

请参阅http://wiki.eclipse.org/EMF-FAQ#How_do_I_use_EMF_in_standalone_applications_.28such_as_an_ordinary_main.29.3F

对于未找到包的问题,​​根据您的情况有两种可能性:

  • 如果您正在使用静态元模型(从您的ecore模型生成java实现),则只需访问相应的Package实例即可将其加载并注册到全局EMF包注册表中。

YourPackage packageInstance = YourPackage.eInstance;

  • 如果您使用的是动态元模型(不生成Java代码),则必须手动注册。
 resourceSet.getPackageRegistry().put(yourPackage.getNsURI(), yourPackage); 

使用前面的代码,您必须先以编程方式从ecore模型中检索EPackage。