0

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

4

2 回答 2

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
于 2014-06-02T18:37:40.820 回答
0

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

  1. 在您的ecore 图中,右键单击您的根节点(相当于您的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>

当然,这可以通过编程方式进行。

于 2014-07-29T20:09:15.120 回答