【问题标题】:EMF - Convert XML model to XMIEMF - 将 XML 模型转换为 XMI
【发布时间】:2014-07-21 12:37:16
【问题描述】:

我从 .xsd 文件创建了一个 .ecore 和 .genmodel 元模型。我正在尝试从符合 .xsd 文件(并因此符合 .ecore 元模型)的 .xml 文件创建模型实例。我怎样才能实现这个目标?

【问题讨论】:

    标签: eclipse-emf emf xmi ecore


    【解决方案1】:

    只有您必须在 EMF 资源中加载 XML 文件,将加载选项 XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT 设置为 true。之后,您必须创建一个输出资源,将 URI 设置为您的文件.xmi。最后,您从 XML 模型资源中获取根元素并将其插入到 XMI 模型资源中,然后保存输出模型并完成。

    Resource loadResource = new ResourceImpl(sourceURI); //We create a resource with XML file uri as parameter, to load de XML model.
    
    // Set option to load configuration file
    Map options = new HashMap(); 
    // The option below deleted Document root in output file
    options.put(XMLResource.OPTION_SUPPRESS_DOCUMENT_ROOT, true);
    loadResource.load(options); // Now we have load the XML model
    
    // Create an output resource where copy element from input resource
    Resource resourceOut = new Resource(targetURI); //We create a resource to XMI file
    
    // Copying elements from input resource to output resource
    EList<EObject> listObj = loadResource.getContents();
    EObject obj = listObj.get(0);
    resourceOut.getContents().add(obj);
    
    resourceOut.save() //We serialize the resource to the XMI file
    

    【讨论】:

    • 我已经实现了您的解决方案,但我正在获取我的 XML 的相同内容。不同之处在于生成的 XMI 的扩展。我使用 Java 创建了一个简单的解析器,但我认为这不是最好的解决方案。你有什么想法吗?
    【解决方案2】:

    最后我只需要更改根节点名称。要实现此目标,您只需执行以下步骤:

    1. 在您的生态图表中,右键单击您的根节点(相当于 xml 文件中的根节点)。
    2. 点击创建动态实例。
    3. 制作一个测试模型。该模型是一个 XMI 实例。
    4. 最后,您只需将您的 rood 节点信息更改为新的(在 XMI 模型中生成的)。

    在我的情况下,我替换了

    /* At XML file */
    
    <featureModel>
    //Here you find the model nodes
    ...
    </ featureModel>
    

    /* XML file converted to XMI file. This file conforms to XSD and ecore model. */
    
    <ide:FeatureModelType [here you will find some attributes]>
    //Here you find the model nodes just as they where defined earlier
    ...
    </ide:FeatureModelType>
    

    当然这可以通过编程来实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 2011-08-21
      • 2015-01-12
      • 2015-05-11
      • 1970-01-01
      相关资源
      最近更新 更多