简体   繁体   English

使用EMF加载和解析XMI文件

[英]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. 我需要通过EMF加载和解析xmi文件(版本:OMG XMI 2.4.1),但现在遇到了问题。

In OMG XMI Spec 2.4.1 , there is a XMI schema XMI.xsd , and a XMI-model.xmi . OMG XMI Spec 2.4.1中 ,有一个XMI模式XMI.xsd和一个XMI-model.xmi So here for instance, I need to load and parse XMI-model.xmi by EMF. 因此,例如,在这里,我需要通过EMF加载和解析XMI-model.xmi

What I have done as follow: 我所做的如下:

  • New an EMF project in Eclipse 在Eclipse中新建一个EMF项目
  • Import xmi schema: XMI.xsd , and get an xmi.genmodel file 导入xmi模式: XMI.xsd ,并获取xmi.genmodel文件
  • Use xmi.genmodel to generate all code in eclipse, it will generate the model code 使用xmi.genmodel生成eclipse中的所有代码,它将生成模型代码
  • 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. 新建一个Junit4测试用例,并尝试使用Junit插件测试加载XMI-model.xmi文件,但失败了,以下是我的代码。

     @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. 好的,然后我尝试读取xsd文件和xmi文件,我发现XMI-model.xmi内部有一个命名空间xmlns:uml="http://www.omg.org/spec/UML/20110701"我去了uml空间的链接,试图找到UML的架构。 Unfortunately, in this link, all the files about the UML is en format XMI. 不幸的是,在此链接中,有关UML的所有文件均为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. 尝试通过使用Enterprise Architect和Altova XMLspy将UML.xmi转换为UML.xsd ,并执行与“失败”相同的操作。 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. 您可以在OMG网站XMI XSD 2.1.1上找到XMI XSD,但您还需要UML架构和MOF架构。 A better way to read and write UML XMI files is to use the Eclipse UML implementation. 读写UML XMI文件的更好方法是使用Eclipse UML实现。 It can easily be used outside Eclipse in a simple Java program : see 它可以在Eclipse之外的简单Java程序中轻松使用:请参阅

I followed the answer and read the documentation about Eclipse UML implementation, but I thought it is not what I want exactly. 我遵循了答案,并阅读了有关Eclipse UML实现的文档,但我认为这并不是我想要的。

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"). 根据您的描述,我无法告诉您确切的上下文,但是XMI通常用作元模型实例(类似于“逻辑模式”)的序列化格式(即“物理模式”)。

But what you are trying to do is using XMI as a metamodel, and use XML (with XSD schema) as serialization format. 但是,您要尝试使用XMI作为元模型,并使用XML(带有XSD架构)作为序列化格式。 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) . 为了加载使用XMI序列化的UML元模型的实例,只需创建一个资源集并使用resourceSet.getResource(uri, true)加载您的.uml文件。 As long as your Eclipse plugin's classpath contains the UML plugin as a dependency, this should work. 只要您的Eclipse插件的类路径包含UML插件作为依赖项,它就应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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