Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to serialize ecore objects to an xml file (serializing ecore model objects that directly reside inside a packgae without a root container object )
How to serialize ecore objects to an xml file [message #1694940] Sat, 09 May 2015 22:02 Go to next message
Hamid Qartal is currently offline Hamid QartalFriend
Messages: 33
Registered: December 2013
Member
Hi,

I am looking to see how is it possible to serialize a set of ecore model objects in an xml file (using Dynamic EMF API). The thing is that my objects are typed over an EClass (namely, Person EClass) that directly resides in my ecore package without any EClass acting as a whole model container.

The following is my ecore file:
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="myTest" nsURI="http://gholizadeh.net" nsPrefix="test">
  <eClassifiers xsi:type="ecore:EClass" name="Person">
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="age" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"/>
  </eClassifiers>
</ecore:EPackage>


For example, in the following code,

EList<EObject> ModelObjects = new BasicEList<EObject>(); 
for (int i = 10; i < 40; i=i+5) {
	//create an instance of a Person Class
	EObject personObject = univInstance.create(personEClass);
          ....
	ModelObjects.add(personObject);
}			
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*", new  XMLResourceFactoryImpl());
Resource univ1 = resourceSet.createResource(URI.createURI("./myTest1.xml"));
univ1.getContents().addAll(ModelObjects);
try{	
    univ1.save(null);
}
catch (IOException e) {e.printStackTrace();}


I add 6 person to my model, but when I save it, only the first element is stored in my XML file as bellow:

<?xml version="1.0" encoding="ASCII"?>
<test:Person xmlns:test="http://gholizadeh.net" age="10"/>

I know this happens because XML file can only have one root element, but how can I force it to make a dummy root and store all my objects. ( I already know that it is possible to create a dummy container class in my metamodel to act as a root, but I am wondering if there is any other way to solve this without this workaround. (In fact, I do not want to change my metamodel.)

As a side question, I see that in many ecore metamodels there is such a dummy EClass that acts as a direct or indirect container of all other EClasses. I am wondering if that is only because of the serialization problem (like the one I have here) or there is other reasons behind that.

Thanks


[Updated on: Sat, 09 May 2015 23:48]

Report message to a moderator

Re: How to store a set of objects of ecore in XML that do not have a container [message #1694944 is a reply to message #1694940] Sun, 10 May 2015 08:01 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30370
Registered: July 2009
Senior Member
Hamid,

Comments below.

On 10/05/2015 12:02 AM, Hamid Qartal wrote:
> Hi,
>
> I am looking to see how is it possible to store a set of ecore
> Eobjects that do not have a container in an xml file (using Dynamic
> EMF API).
> For example, in the following code,
>
>
> EList<EObject> ModelObjects = new BasicEList<EObject>(); for (int i =
> 10; i < 40; i=i+5) {
> //create an instance of a Person Class
> EObject personObject = univInstance.create(personEClass);
> ....
> ModelObjects.add(personObject);
> }
> resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("*",
> new XMLResourceFactoryImpl());
An XMLResourceImpl will save only one root object. Use XMIResourceImpl
if you want more than one...
> Resource univ1 =
> resourceSet.createResource(URI.createURI("./myTest1.xml"));
> univ1.getContents().addAll(ModelObjects);
> try{
> univ1.save(null);
> }
> catch (IOException e) {e.printStackTrace();}
>
>
> I add 6 person to my model, but when I save it, only the first element
> is stored in my XML file as bellow:
>
>
> <?xml version="1.0" encoding="ASCII"?>
> <test:Person xmlns:test="http://gholizadeh.net" age="10"/>
>
> I know this happens because XML file can only have one root element,
> but how can I force it to make a dummy root and store all my objects.
That's what XMIResourceImpl does.
> ( I already know that it is possible to create a dummy container class
> in my metamodel to act as a root, but I am wondering if there is any
> other way to solve this without this workaround. (In fact, I do not
> want to change my metamodel.)
>
> As a side question, I see that in many metamodels of ecore there is
> such a dummy Class that acts as a direct or indirect container of all
> other classes. I am wondering if that is only because of the
> persistence problem or there is other reasons behind that.
XML itself only allows one root element so to save multiple elements
there must be a dummy single root element.
>
> Thanks
>
>
>
Previous Topic:Editing a cdo model on a client programmatically
Next Topic:Missing "new child" menu entry
Goto Forum:
  


Current Time: Fri Aug 23 05:58:24 GMT 2019

Powered by FUDForum. Page generated in 0.01536 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top