XMLResource Save Problem [message #1268932] |
Tue, 11 March 2014 06:48 |
|
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 |
Ed Merks 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/
|
|
|
Powered by
FUDForum. Page generated in 0.02646 seconds