Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Persisting ecore instance as XML only persists first node of model
Persisting ecore instance as XML only persists first node of model [message #431928] Wed, 29 July 2009 19:16 Go to next message
Derek Palma is currently offline Derek PalmaFriend
Messages: 141
Registered: July 2009
Senior Member
Hi,

I have been persisting my models with XMI and it has been working fine. I
wanted to start using XML to make the models easier to parse for some
usages.

When I persist with GenericXMLResourceFactory only the first (or root I
guess) object gets written to the xml file.

My models are generated using dynamic EMF, the EObjects copied to a resource
created with GenericXMLFactory, and then written.

Below is a simple example I created which demonstrates this problem. It
creates a single EObject which contains a containment reference to a second
EObject.

After stepping into XMLResourceImpl it looks like the code writes the first
EOjbect in the Resource and never wants to later write the rest. I presume I
must have some option not set correctly or such.

Any insight is greatly appreciated.

Thanks in advance.
Derek


Resulting XML:

<?xml version="1.0" encoding="UTF-8"?>
<de:ContainingClass xmlns:de="http://test/dynamic/EMF" myAttr2="containing
class attribute value">
<myRef2 href="out.xml#/1"/>
</de:ContainingClass>




public class DynamicEMFPersistenceTest {

private EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
private EPackage ePackage = ecoreFactory.createEPackage();

private void createEcoreModel() {
ePackage.setName("dynamicEMF");
ePackage.setNsPrefix("de");
ePackage.setNsURI("http://test/dynamic/EMF");

// Create a contained class
String containedClassName = "ContainedClass";
EClass containedEClass = this.ecoreFactory.createEClass();
containedEClass.setName(containedClassName);

EAttribute eAttr = ecoreFactory.createEAttribute();
final String myAttrName = "containedClassAttr";
eAttr.setName(myAttrName);
eAttr.setEType(EcorePackage.Literals.ESTRING);
containedEClass.getEStructuralFeatures().add(eAttr);

ePackage.getEClassifiers().add(containedEClass);

// Create the containing class
String containingClassName = "ContainingClass";
EClass containingEClass = this.ecoreFactory.createEClass();
containingEClass.setName(containingClassName);

EAttribute eAttr2 = ecoreFactory.createEAttribute();
final String myAttrName2 = "myAttr2";
eAttr2.setName(myAttrName2);
eAttr2.setEType(EcorePackage.Literals.ESTRING);
containingEClass.getEStructuralFeatures().add(eAttr2);

EReference eRef = ecoreFactory.createEReference();
String myRefName = "myRef2";
eRef.setName(myRefName);
eRef.setEType(containedEClass);
eRef.setContainment(true);
containingEClass.getEStructuralFeatures().add(eRef);

ePackage.getEClassifiers().add(containingEClass);


EObject containedClassObject =
ePackage.getEFactoryInstance().create(containedEClass);
EStructuralFeature attrFeature =
containedEClass.getEStructuralFeature(myAttrName);
containedClassObject.eSet(attrFeature, "contained class attribute value");

EObject containingClassObject =
ePackage.getEFactoryInstance().create(containingEClass);
EStructuralFeature attrFeature2 =
containingEClass.getEStructuralFeature(myAttrName2);
containingClassObject.eSet(attrFeature2, "containing class attribute
value");

EStructuralFeature refFeature =
containingEClass.getEStructuralFeature(myRefName);
containingClassObject.eSet(refFeature, containedClassObject);

EPackage.Registry.INSTANCE.put(ePackage.getNsURI(), ePackage);
URI uri = URI.createFileURI("out.xml");
Resource resource = createXmlResource(uri);
resource.getContents().add(containingClassObject);
resource.getContents().add(containedClassObject);
saveResource(resource);
}

public static void saveResource(Resource resource) {
try {
resource.save(null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private Resource createXmlResource(URI uri) {
// createGlobalEPackageRegistry();
// EMFUtil.dumpGlobalEPackageRegistry();
ResourceSet resourceSet = new ResourceSetImpl();

final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(
resourceSet.getPackageRegistry());
resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED _META_DATA,
extendedMetaData);
resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
.put("xml", new GenericXMLResourceFactoryImpl());

Resource resource = resourceSet.createResource(uri);
return resource;
}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DynamicEMFPersistenceTest inst = new DynamicEMFPersistenceTest();
inst.createEcoreModel();

}

}
Re: Persisting ecore instance as XML only persists first node of model [message #431935 is a reply to message #431928] Thu, 30 July 2009 05:52 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 5590
Registered: July 2009
Senior Member
Derek,

In contrast to XMI you can only store a single root object in an XML
document. I guess EMF just takes resource.getContents().get(0). With XML
you need to explicitely add your (former) root objects to a container
object (an instance of one of your modeled classes) and make that the
new, single root object of the resource.

Cheers
/Eike

----
http://thegordian.blogspot.com
http://twitter.com/eikestepper



Derek Palma schrieb:
> Hi,
>
> I have been persisting my models with XMI and it has been working
> fine. I wanted to start using XML to make the models easier to parse
> for some usages.
>
> When I persist with GenericXMLResourceFactory only the first (or root
> I guess) object gets written to the xml file.
>
> My models are generated using dynamic EMF, the EObjects copied to a
> resource created with GenericXMLFactory, and then written.
>
> Below is a simple example I created which demonstrates this problem.
> It creates a single EObject which contains a containment reference to
> a second EObject.
>
> After stepping into XMLResourceImpl it looks like the code writes the
> first EOjbect in the Resource and never wants to later write the rest.
> I presume I must have some option not set correctly or such.
>
> Any insight is greatly appreciated.
>
> Thanks in advance.
> Derek
>
>
> Resulting XML:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <de:ContainingClass xmlns:de="http://test/dynamic/EMF"
> myAttr2="containing class attribute value">
> <myRef2 href="out.xml#/1"/>
> </de:ContainingClass>
>
>
>
>
> public class DynamicEMFPersistenceTest {
>
> private EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
> private EPackage ePackage = ecoreFactory.createEPackage();
>
> private void createEcoreModel() {
> ePackage.setName("dynamicEMF");
> ePackage.setNsPrefix("de");
> ePackage.setNsURI("http://test/dynamic/EMF");
>
> // Create a contained class
> String containedClassName = "ContainedClass";
> EClass containedEClass = this.ecoreFactory.createEClass();
> containedEClass.setName(containedClassName);
>
> EAttribute eAttr = ecoreFactory.createEAttribute();
> final String myAttrName = "containedClassAttr";
> eAttr.setName(myAttrName);
> eAttr.setEType(EcorePackage.Literals.ESTRING);
> containedEClass.getEStructuralFeatures().add(eAttr);
>
> ePackage.getEClassifiers().add(containedEClass);
>
> // Create the containing class
> String containingClassName = "ContainingClass";
> EClass containingEClass = this.ecoreFactory.createEClass();
> containingEClass.setName(containingClassName);
>
> EAttribute eAttr2 = ecoreFactory.createEAttribute();
> final String myAttrName2 = "myAttr2";
> eAttr2.setName(myAttrName2);
> eAttr2.setEType(EcorePackage.Literals.ESTRING);
> containingEClass.getEStructuralFeatures().add(eAttr2);
>
> EReference eRef = ecoreFactory.createEReference();
> String myRefName = "myRef2";
> eRef.setName(myRefName);
> eRef.setEType(containedEClass);
> eRef.setContainment(true);
> containingEClass.getEStructuralFeatures().add(eRef);
>
> ePackage.getEClassifiers().add(containingEClass);
>
>
> EObject containedClassObject =
> ePackage.getEFactoryInstance().create(containedEClass);
> EStructuralFeature attrFeature =
> containedEClass.getEStructuralFeature(myAttrName);
> containedClassObject.eSet(attrFeature, "contained class attribute
> value");
>
> EObject containingClassObject =
> ePackage.getEFactoryInstance().create(containingEClass);
> EStructuralFeature attrFeature2 =
> containingEClass.getEStructuralFeature(myAttrName2);
> containingClassObject.eSet(attrFeature2, "containing class attribute
> value");
>
> EStructuralFeature refFeature =
> containingEClass.getEStructuralFeature(myRefName);
> containingClassObject.eSet(refFeature, containedClassObject);
>
> EPackage.Registry.INSTANCE.put(ePackage.getNsURI(), ePackage);
> URI uri = URI.createFileURI("out.xml");
> Resource resource = createXmlResource(uri);
> resource.getContents().add(containingClassObject);
> resource.getContents().add(containedClassObject);
> saveResource(resource);
> }
>
> public static void saveResource(Resource resource) {
> try {
> resource.save(null);
> } catch (IOException e) {
> // TODO Auto-generated catch block
> e.printStackTrace();
> }
> }
>
> private Resource createXmlResource(URI uri) {
> // createGlobalEPackageRegistry();
> // EMFUtil.dumpGlobalEPackageRegistry();
> ResourceSet resourceSet = new ResourceSetImpl();
>
> final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(
> resourceSet.getPackageRegistry());
> resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED _META_DATA,
> extendedMetaData);
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
> .put("xml", new GenericXMLResourceFactoryImpl());
>
> Resource resource = resourceSet.createResource(uri);
> return resource;
> }
>
> /**
> * @param args
> */
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> DynamicEMFPersistenceTest inst = new DynamicEMFPersistenceTest();
> inst.createEcoreModel();
>
> }
>
> }
Re: Persisting ecore instance as XML only persists first node of model [message #431952 is a reply to message #431935] Thu, 30 July 2009 14:09 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 26289
Registered: July 2009
Senior Member
Eike,

That's right. One should use XMIResourceFactoryImpl to create an
XMIResourceImpl to serialize multiple roots.


Eike Stepper wrote:
> Derek,
>
> In contrast to XMI you can only store a single root object in an XML
> document. I guess EMF just takes resource.getContents().get(0). With XML
> you need to explicitely add your (former) root objects to a container
> object (an instance of one of your modeled classes) and make that the
> new, single root object of the resource.
>
> Cheers
> /Eike
>
> ----
> http://thegordian.blogspot.com
> http://twitter.com/eikestepper
>
>
>
> Derek Palma schrieb:
>
>> Hi,
>>
>> I have been persisting my models with XMI and it has been working
>> fine. I wanted to start using XML to make the models easier to parse
>> for some usages.
>>
>> When I persist with GenericXMLResourceFactory only the first (or root
>> I guess) object gets written to the xml file.
>>
>> My models are generated using dynamic EMF, the EObjects copied to a
>> resource created with GenericXMLFactory, and then written.
>>
>> Below is a simple example I created which demonstrates this problem.
>> It creates a single EObject which contains a containment reference to
>> a second EObject.
>>
>> After stepping into XMLResourceImpl it looks like the code writes the
>> first EOjbect in the Resource and never wants to later write the rest.
>> I presume I must have some option not set correctly or such.
>>
>> Any insight is greatly appreciated.
>>
>> Thanks in advance.
>> Derek
>>
>>
>> Resulting XML:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <de:ContainingClass xmlns:de="http://test/dynamic/EMF"
>> myAttr2="containing class attribute value">
>> <myRef2 href="out.xml#/1"/>
>> </de:ContainingClass>
>>
>>
>>
>>
>> public class DynamicEMFPersistenceTest {
>>
>> private EcoreFactory ecoreFactory = EcoreFactory.eINSTANCE;
>> private EPackage ePackage = ecoreFactory.createEPackage();
>>
>> private void createEcoreModel() {
>> ePackage.setName("dynamicEMF");
>> ePackage.setNsPrefix("de");
>> ePackage.setNsURI("http://test/dynamic/EMF");
>>
>> // Create a contained class
>> String containedClassName = "ContainedClass";
>> EClass containedEClass = this.ecoreFactory.createEClass();
>> containedEClass.setName(containedClassName);
>>
>> EAttribute eAttr = ecoreFactory.createEAttribute();
>> final String myAttrName = "containedClassAttr";
>> eAttr.setName(myAttrName);
>> eAttr.setEType(EcorePackage.Literals.ESTRING);
>> containedEClass.getEStructuralFeatures().add(eAttr);
>>
>> ePackage.getEClassifiers().add(containedEClass);
>>
>> // Create the containing class
>> String containingClassName = "ContainingClass";
>> EClass containingEClass = this.ecoreFactory.createEClass();
>> containingEClass.setName(containingClassName);
>>
>> EAttribute eAttr2 = ecoreFactory.createEAttribute();
>> final String myAttrName2 = "myAttr2";
>> eAttr2.setName(myAttrName2);
>> eAttr2.setEType(EcorePackage.Literals.ESTRING);
>> containingEClass.getEStructuralFeatures().add(eAttr2);
>>
>> EReference eRef = ecoreFactory.createEReference();
>> String myRefName = "myRef2";
>> eRef.setName(myRefName);
>> eRef.setEType(containedEClass);
>> eRef.setContainment(true);
>> containingEClass.getEStructuralFeatures().add(eRef);
>>
>> ePackage.getEClassifiers().add(containingEClass);
>>
>>
>> EObject containedClassObject =
>> ePackage.getEFactoryInstance().create(containedEClass);
>> EStructuralFeature attrFeature =
>> containedEClass.getEStructuralFeature(myAttrName);
>> containedClassObject.eSet(attrFeature, "contained class attribute
>> value");
>>
>> EObject containingClassObject =
>> ePackage.getEFactoryInstance().create(containingEClass);
>> EStructuralFeature attrFeature2 =
>> containingEClass.getEStructuralFeature(myAttrName2);
>> containingClassObject.eSet(attrFeature2, "containing class attribute
>> value");
>>
>> EStructuralFeature refFeature =
>> containingEClass.getEStructuralFeature(myRefName);
>> containingClassObject.eSet(refFeature, containedClassObject);
>>
>> EPackage.Registry.INSTANCE.put(ePackage.getNsURI(), ePackage);
>> URI uri = URI.createFileURI("out.xml");
>> Resource resource = createXmlResource(uri);
>> resource.getContents().add(containingClassObject);
>> resource.getContents().add(containedClassObject);
>> saveResource(resource);
>> }
>>
>> public static void saveResource(Resource resource) {
>> try {
>> resource.save(null);
>> } catch (IOException e) {
>> // TODO Auto-generated catch block
>> e.printStackTrace();
>> }
>> }
>>
>> private Resource createXmlResource(URI uri) {
>> // createGlobalEPackageRegistry();
>> // EMFUtil.dumpGlobalEPackageRegistry();
>> ResourceSet resourceSet = new ResourceSetImpl();
>>
>> final ExtendedMetaData extendedMetaData = new BasicExtendedMetaData(
>> resourceSet.getPackageRegistry());
>> resourceSet.getLoadOptions().put(XMLResource.OPTION_EXTENDED _META_DATA,
>> extendedMetaData);
>> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap()
>> .put("xml", new GenericXMLResourceFactoryImpl());
>>
>> Resource resource = resourceSet.createResource(uri);
>> return resource;
>> }
>>
>> /**
>> * @param args
>> */
>> public static void main(String[] args) {
>> // TODO Auto-generated method stub
>> DynamicEMFPersistenceTest inst = new DynamicEMFPersistenceTest();
>> inst.createEcoreModel();
>>
>> }
>>
>> }
>>
Previous Topic:Getter/Setter methods when dynamically build the model
Next Topic:EMF Meta Tooling
Goto Forum:
  


Current Time: Mon Dec 22 14:21:50 GMT 2014

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

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