How to convert XSD to Ecore (EMF)
Asked Answered
I

4

11

What is the best way to convert .xsd-files into .ecore-files?

Is there an Eclipse plugin for that?

Inceptive answered 22/3, 2009 at 20:19 Comment(0)
I
11

That's what worked for me:

  • New -> Project...
  • Eclipse Modeling Framework -> EMF Project
  • Model Importers: XML Schema
  • Model URIs: [Select xsd-File]

To revalidate the .ecore-File when xsd has changed:

  • Right-Click on .genmodel-File
  • Reload...
Inceptive answered 23/3, 2009 at 17:1 Comment(3)
In Galileo you have to install the feature "XSD Ecore Converter". This provides the model importer for XML Schema.Cutaneous
In never versions of Eclipse, this feature is contained in the "XSD - XML Schema Definition SDK" feature.Tribulation
In the newer versions (neon 3 in my case), I needed to install XML schema definition. So I can see the XML schema importer.Frear
P
7

If you do not want to create a new MDT project every time you want to import a schema as ECore model then there is also another way to do this:

  • New -> EMF Generator Model (in "Eclipse Modelling Framework")
  • Press Next
  • Select folder and specify filename (has to have the extension "genmodel")
  • Press Next
  • Select "XML Schema" as model importer
  • Press Next
  • Select URI to your XSD
  • (Optionally, select tick box "Create XML Schema to Ecore Map" if you want to generate a .xsd2ecore map file)
  • Press Next
  • Select all desired root packages
  • Press Finish
Precursory answered 23/5, 2012 at 12:11 Comment(0)
B
4

An example class. I did not clean up the imports.

 

import org.eclipse.emf.common.util.URI;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import org.eclipse.emf.ecore.*;
import org.eclipse.xsd.*;
import org.eclipse.xsd.ecore.XSDEcoreBuilder;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.*;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.edit.ui.*;


public class Xsd2Ecore {

    public static void main(String[] args) {
        Xsd2Ecore x2e = new Xsd2Ecore();
        x2e.go("UMLVersions/V1.0.0/UML2XMI.xsd", "UMLVersions/V1.0.0/UML2100.xmi");
    }


    public void go(String sourcename, String targetname) {

        System.out.println("Starting");

        XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
        ResourceSet resourceSet = new ResourceSetImpl();
        Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI(sourcename));

        resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xmi", new XMIResourceFactoryImpl());
        Resource resource = resourceSet.createResource(URI.createFileURI(targetname));

        for (Iterator iter = eCorePackages.iterator(); iter.hasNext();) {
            EPackage element = (EPackage) iter.next();
            resource.getContents().add(element);
        }

        try {
            resource.save(null);
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("Finished");

    }

}

Buckish answered 6/1, 2011 at 10:41 Comment(0)
M
0

Have you tried

eclipse –console –noExit –noSplash -data C:\temp\emf-ws
    -application org.eclipse.xsd.ecore.importer.XSD2GenModel

It generates .ecore and .genmodel for your set of XSDs.

Mellman answered 12/8, 2014 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.