How to convert XSD to Ecore (EMF)

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

Is there an Eclipse plugin for this?

+7
eclipse-plugin xsd eclipse-emf eclipse-emf-ecore
source share
4 answers

What worked for me:

  • New → Project ...
  • Eclipse Modeling Framework → EMF Project
  • Import Models: XML Schema
  • Model URI : [Select xsd file]

To re-check the .ecore file when changing xsd:

  • Right click on .genmodel-File
  • Reload ...
+8
source share

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

  • New → EMF Generator Model (in the "Eclipse Modeling Framework")
  • Click "Next"
  • Select a folder and specify a file name (you must have the extension "genmodel")
  • Click "Next"
  • Select " XML Schema " as the Importer of the Model
  • Click "Next"
  • Choose a URI for XSD
  • (If desired, select the checkbox "Create XML Schema in Ecore Map" if you want to generate a map file .xsd2ecore)
  • Click "Next"
  • Select all required root packages
  • Click Finish
+6
source share

An approximate class. I did not clear the import.

 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"); } } 
+3
source share

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 XSD set.

0
source share

All Articles