动态加载EMF模型时出错

时间:2014-02-12 00:16:26

标签: java eclipse eclipse-emf metamodel

我正在关注文章http://www.ibm.com/developerworks/library/os-eclipse-dynamicemf/以动态加载元模型。

我使用此

加载模型实例化文档
ResourceSet load_resourceSet = new ResourceSetImpl();
// ResourceSet load_resourceSet2 = new ResourceSetImpl();

/*
 * Register XMI Factory impl ementation using DEFAULT_EXTENSION
 */
load_resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", //$NON-NLS-1$
            new XMIResourceFactoryImpl());

/*
 * Add bookStoreEPackage to package registry
 */
load_resourceSet.getPackageRegistry().put("http:///com.ibm.dynamic.example.bookstore.ecore",
        bookStoreEPackage);

//  load_resourceSet2.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", //$NON-NLS-1$
    // new XMIResourceFactoryImpl());

/*
 * Load the resources using the URI
 */
Resource modelo_esquerda = load_resourceSet
                .getResource(URI.createURI("./BookStore.xmi"), true);

但是,我收到了此错误消息

Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.ClassNotFoundException: Class 'BookStore' is not found or is abstract. (.\BookStore.xmi, 9, 34)

XMI文件已存在于目录中。

我能做什么?

谢谢

1 个答案:

答案 0 :(得分:2)

您可以尝试以下方法,它对我有用:

XMIResourceImpl resource = new XMIResourceImpl();
File source = new File(xmlName.xml);
resource.load(new FileInputStream(source), new HashMap<Object,Object>());
Data data = (Data) resource.getContents().get(0);

Data是你的模特。