简体   繁体   中英

Load and parse XMI file with EMF

I need to load and parse the xmi file(version: OMG XMI 2.4.1) by EMF, but now get stuck.

In OMG XMI Spec 2.4.1 , there is a XMI schema XMI.xsd , and a XMI-model.xmi . So here for instance, I need to load and parse XMI-model.xmi by EMF.

What I have done as follow:

  • New an EMF project in Eclipse
  • Import xmi schema: XMI.xsd , and get an xmi.genmodel file
  • Use xmi.genmodel to generate all code in eclipse, it will generate the model code
  • New a Junit4 test case and try to load the XMI-model.xmi file with Junit plug-in test but failed, following is my code.

     @Test public void test() throws IOException { URI uri = URI .createFileURI("/home/pzhengshuai/Desktop/15Feb-19Feb/XMI-model.xmi"); _20110701ResourceFactoryImpl factory = new _20110701ResourceFactoryImpl(); Resource resource = factory.createResource(uri); Map<String, Boolean> options = new HashMap<String, Boolean>(); options.put(XMLResource.OPTION_KEEP_DEFAULT_CONTENT, Boolean.TRUE); resource.load(options); EObject model = (EObject) resource.getContents().get(0); Assert.assertNotNull(model); } 

The exception is:

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Feature 'XMIType' not found. (file:/home/pzhengshuai/Desktop/15Feb-19Feb/XMI-model.xmi, 2, 164)

Okay, then I tried to read the xsd file and xmi file, I found there is a namespace xmlns:uml="http://www.omg.org/spec/UML/20110701" inside the XMI-model.xmi , so I went to the link of the uml space and tried to found the schema of UML. Unfortunately, in this link, all the files about the UML is en format XMI.

So I tried two more option:

  1. try to convert UML.xmi to UML.xsd by using Enterprise Architect, and Altova XMLspy, and do the same stuff above, Failed. Personally, I think this convertion may lost some information.
  2. I found a similiar question here ,

You can find the XMI XSD on the OMG website XMI XSD 2.1.1 but you will also need the UML Schema and the MOF Schema. A better way to read and write UML XMI files is to use the Eclipse UML implementation. It can easily be used outside Eclipse in a simple Java program : see

I followed the answer and read the documentation about Eclipse UML implementation, but I thought it is not what I want exactly.

So I hope some one could give me some advices. Thanks in advance for help!

From your description I can't tell your exact context, but XMI is usually used as the serialization format (ie a "physical schema") for an instance of a metamodel (which is something like a "logical schema").

But what you are trying to do is using XMI as a metamodel, and use XML (with XSD schema) as serialization format. That's probably not what you want.

In order to load an instance of the UML metamodel which is serializied using XMI, just create a resource set and load your .uml file using resourceSet.getResource(uri, true) . As long as your Eclipse plugin's classpath contains the UML plugin as a dependency, this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM