Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » XMLResource Save Problem
XMLResource Save Problem [message #1268932] Tue, 11 March 2014 06:48 Go to next message
Dollyn Sun is currently offline Dollyn SunFriend
Messages: 68
Registered: July 2009
Location: China
Member


Hi,
I'm using the following code to write some EObjects to a file:

		Resource resource = new XMLResourceImpl();

		resource.getContents().add(eobject1);
		resource.getContents().add(eobject2);

                resource.save(stream, null);


But the result is that only eobject1 is serialized to the file. If I use the XMIResoruceImpl, the result is right.

So I looked into the "save" source code, and find out those code in class "XMLSaveImpl":
    int size = contents.size();

    Object mark;

    if (size == 1)
    {
      mark = writeTopObject(contents.get(0));
    }
    else
    {
      mark = writeTopObjects(contents);
    }


  protected Object writeTopObjects(List<? extends EObject> contents)
  {
    return writeTopObject(contents.get(0));
  }


That is confusing... Why does XMLSaveImpl only serialize the first element in the Resource.getContents() list?


Dollyn
Re: XMLResource Save Problem [message #1268940 is a reply to message #1268932] Tue, 11 March 2014 06:59 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Dollyn,

Comments below.

On 11/03/2014 7:48 AM, Dollyn Sun wrote:
>
> Hi,
> I'm using the following code to write some EObjects to a file:
>
> Resource resource = new XMLResourceImpl();
Resources generally should have a URI and it's typically better to
ensure there is a resource set and use ResourceSet.createResource to
create resources.
>
> resource.getContents().add(eobject1);
> resource.getContents().add(eobject2);
>
> resource.save(stream, null);
>
> But the result is that only eobject1 is serialized to the file. If I
> use the XMIResoruceImpl, the result is right.
Use XMIResourceImpl if you want to support multiple root objects.
>
> So I looked into the "save" source code, and find out those code in
> class "XMLSaveImpl":
> int size = contents.size();
>
> Object mark;
>
> if (size == 1)
> {
> mark = writeTopObject(contents.get(0));
> }
> else
> {
> mark = writeTopObjects(contents);
> }
>
> protected Object writeTopObjects(List<? extends EObject> contents)
> {
> return writeTopObject(contents.get(0));
> }
>
> That is confusing... Why does XMLSaveImpl only serialize the first
> element in the Resource.getContents() list?
Because XML only allows one root element; only XMI resources will
introduce a "fake" root element to contain the multiple objects in the
resource.


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Getting the org.eclipse.emf.ecore.resource.Resource from the Active Editor
Next Topic:Having a reference specify itself for the EOpposite
Goto Forum:
  


Current Time: Fri Apr 19 21:54:13 GMT 2024

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

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

Back to the top