In this post I will show you how to create an EMF Ecore Model from .xsd file. After that we will use the model to create an Xpand project, which will allow us to generate code, which is based on that model. This project will use .xml files as an input and will generate a code, based on their content. The tutorial assumes that the reader has installed the Eclipse Modeling Tools. Check the previous post on how to install the IDE. In that post you can also find the required .xml files.
Getting the XSD
The first thing that we need is the .xsd file, which we will use to generate the EMF Ecore model. If the .xsd file is not available, we can generate it from the corresponding .xml file. There are some online converters, which we can use to generated the schema of a given .xml file. I am using this one in my projects. So all you have to do is to copy the content from one of the previously provided .xml files (or your own .xml files), then paste it in the website and hit the Generate button. After the generation is over, you will have the .xsd file. Save it on your side.
Create EMF Ecore Model 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.
Then 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. You can leave everything else as it is.
The project is generated successfully. Hit the Finish button. The project structure would look something like this:
Generated EMF Ecore Model
There are two files – .ecore and .genmodel. We will use the .ecore file in the Xpand project. The .genmodel file we use to generate Java classes and implementation. The Xpand project will use these classes. In the upcoming post we will see how to update the Ecore model and generate code from it.
This post showed briefly how to generate Ecore model from an XSD schema of a particular .xml files. We used an online .xsd file generator to generate the needed files but of course you can write the file by yourself. In the next post we will use the generated models to generate some Java classes which we will need for the future code generation. Stay tuned.
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
Passionate developer, loving husband and caring father. As an introvert programming is my life. I work as a senior software engineer in a big international company. My hobbies include playing computer games (mostly World of Warcraft), watching TV series and hiking.