reading the eobjects from the ecore file in eclipse

拟墨画扇 提交于 2019-12-02 22:52:54

问题


I have the ecore file which contains the class eobjects.Now i want to read that ecore file and get all the class eobjects from that ecore file.


回答1:


Do you mean you want to reload your specific xmi file with a custom suffix?

Here is an example of a method that loads an ecore file at a specific location (path) and returns your root 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;
}


来源:https://stackoverflow.com/questions/38138898/reading-the-eobjects-from-the-ecore-file-in-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!