In this post I will show you how to create an Ecore model from xsd file. This model will be used later to create an Xpand2 project, which will allow us to generate code, based on that model. This project will use xml files as an input and will generate a code, based on their content. More information about Ecore and EMF can be found here and here. This tutorial assumes that the reader has the Eclipse Modelling Tools installed.
Getting the XSD:
The first thing that we need is the xsd file, which is used to verify the xml files, used an an input. If the xsd file is not available, it can be generated from the corresponding xml file. There are some online converters, which can be used to generated the schema of a given xml file. I am using this one in my projects.
Create EMF Project:
In Eclipse select File > New > Project.
In the New Project window select "EMF Project" and press Next
Give a name for that project and click Next.
In the next window, select XML Schema and click Next
Next step is to provide the file path to the xsd file and load it. Here, Eclipse provides options to create a XML Schema to Ecore Map, but I prefer not to select it, because it will require extra time to generate it and in the end I will most probably not use it at all. Another option is to sort the attributes Alphabetically, which might be useful.
The project is generated successfully. Hit the Finish button. The project structure would look something like this:
There are two files - .ecore and .genmodel. The .ecore file will be used in the Xpand2 project. The .genmodel file is used to generate Java classes and implementation. This classes will be used again by the Xpand project. In order to generate the classes, open the .genmodel file by double clicking on it. Right click the root nood and select Generate Model Code
This will generate the Java classes together with some other utilities.
This post showed briefly how to generate Ecore model and Java classes, based on an XSD schema for a particular xml files. The generated files will be used later by another project to automatically generate executable code, using templates written by the user. In a next post I will show you how to create such a project, write templates and generate code from a given xml file.