我已经从.xsd文件创建了一个.ecore和.genmodel元模型。我正在尝试从符合.xsd文件的.xml文件创建模型实例(结果是.ecore元模型)。我怎样才能实现这个目标?
答案 0 :(得分: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
答案 1 :(得分:0)
最后我只需要更改根节点名称。要实现这一目标,您只需按照以下步骤操作:
在我的情况下,我更换了
/* 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>
当然这可以通过编程方式进行。