从 eclipse 中的 ecore 文件中读取 eobjects

reading the eobjects from the ecore file in eclipse

我有包含 class eobjects.Now 的 ecore 文件,我想读取该 ecore 文件并从该 ecore 文件中获取所有 class eobjects。

您是说要重新加载带有自定义后缀的特定 xmi 文件吗?

这是在特定位置(路径)加载 ecore 文件的方法示例,returns 您的根 EObject

public static EObject loadYourModel(String path) {
    /*Initialzie Models*/
    YourPackage.eINSTANCE.eClass();

    /*register your xmi resources*/
    final Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
    final Map<String, Object> m = reg.getExtensionToFactoryMap();
    /*put all your different ecore file suffixes in the map; suffix = YourPackage.eNAME*/
    m.put(YourPackage.eNAME, new XMIResourceFactoryImpl());
    /*you can put all different package names here*/

    /*Create a new Resource set to store the EObjects from the file*/
    ResourceSet resSet = new ResourceSetImpl();

    /*get the resource of your ecore file*/
    Resource resource = resSet.getResource(URI.createURI(path), true);
    /*Get the first element = root of your model hierachy*/
    EObject root = resource.getContents().get(0);
    return root;
}