Skip to main content



      Home
Home » Modeling » EMF » Serializing a dynamic model instance with xsi:schemaLocation
Serializing a dynamic model instance with xsi:schemaLocation [message #411303] Sat, 21 July 2007 04:28 Go to next message
Eclipse UserFriend
Hi,

I successfully generated a dynamic Ecore model from an XML Schema using
XSDEcoreBuilder, and created its instance using the generated EPackages,
by following the article below:
http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema

How can I serialize the instance with xsi:schemaLocation?

I set OPTION_SCHEMA_LOCATION, but it didn't work. I also tried to set
"xSISchemaLocation" of the DynamicEObject "DocumentRoot" by the following
code, but xsi:schemaLocation did not appear.

EClass documentRootClass = (EClass)
ePackage.getEClassifier("DocumentRoot");
EObject documentRoot = EcoreUtil.create(documentRootClass);
EMap<String, String> map = (EMap<String, String>)
documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
map.put(ePackage.getNsURI(), "sample.xsd");
:

Do I have to serialize the Ecore model as .ecore file in advance?

Thanks,
Koji
Re: Serializing a dynamic model instance with xsi:schemaLocation [message #411306 is a reply to message #411303] Sat, 21 July 2007 08:17 Go to previous messageGo to next message
Eclipse UserFriend
This is a multi-part message in MIME format.
--------------030008080402010707030002
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Koji,

Comments below.


Koji Hashimoto wrote:
> Hi,
>
> I successfully generated a dynamic Ecore model from an XML Schema
> using XSDEcoreBuilder, and created its instance using the generated
> EPackages, by following the article below:
> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema
>
>
> How can I serialize the instance with xsi:schemaLocation?
>
> I set OPTION_SCHEMA_LOCATION, but it didn't work.
You've used this option for save not just load?
> I also tried to set "xSISchemaLocation" of the DynamicEObject
> "DocumentRoot" by the following code, but xsi:schemaLocation did not
> appear.
>
> EClass documentRootClass = (EClass)
> ePackage.getEClassifier("DocumentRoot");
> EObject documentRoot = EcoreUtil.create(documentRootClass);
> EMap<String, String> map = (EMap<String, String>)
> documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
>
Using this would be simpler:

documentRoot.eGet(ExtendedMetaData.INSTANCE.getXSISchemaLoca tionMap(documentRoot.eClass()));


> map.put(ePackage.getNsURI(), "sample.xsd");
> :
>
I would expect the above should definitely do the trick. It does when I
try it.
> Do I have to serialize the Ecore model as .ecore file in advance?
When an instance document is serialized, if the EPackage's nsURI is
different from the EPackage's eResource()'s URI and the
OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will automatically
serialize an xsi:schemaLocation for the nsURI -> packageResourceURI mapping.
>
> Thanks,
> Koji
>


--------------030008080402010707030002
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Koji,<br>
<br>
Comments below.<br>
<br>
<br>
Koji Hashimoto wrote:
<blockquote
cite="mid:3d5840065018a31634fac40ff206be64$1@www.eclipse.org"
type="cite">Hi,
<br>
<br>
I successfully generated a dynamic Ecore model from an XML Schema using
XSDEcoreBuilder, and created its instance using the generated
EPackages, by following the article below:
<br>
<a class="moz-txt-link-freetext" href=" http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema"> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema</a>
<br>
<br>
How can I serialize the instance with xsi:schemaLocation?
<br>
<br>
I set OPTION_SCHEMA_LOCATION, but it didn't work. </blockquote>
You've used this option for save not just load?<br>
<blockquote
cite="mid:3d5840065018a31634fac40ff206be64$1@www.eclipse.org"
type="cite">I also tried to set "xSISchemaLocation" of the
DynamicEObject "DocumentRoot" by the following code, but
xsi:schemaLocation did not appear.
<br>
<br>
EClass documentRootClass = (EClass)
ePackage.getEClassifier("DocumentRoot");
<br>
EObject documentRoot = EcoreUtil.create(documentRootClass);
<br>
EMap&lt;String, String&gt; map = (EMap&lt;String, String&gt;)
documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
<br>
</blockquote>
Using this would be simpler:<br>
<blockquote> documentRoot.eGet(ExtendedMetaData.INSTANCE.getXSISchemaLoca tionMap(documentRoot.eClass()));
</blockquote>
<blockquote
cite="mid:3d5840065018a31634fac40ff206be64$1@www.eclipse.org"
type="cite">map.put(ePackage.getNsURI(), "sample.xsd");
<br>
Re: Serializing a dynamic model instance with xsi:schemaLocation [message #411309 is a reply to message #411306] Sun, 22 July 2007 08:16 Go to previous messageGo to next message
Eclipse UserFriend
Ed,

Thank you for your reply.

> You've used this option for save not just load?
Yes, I have used OPTION_SCHEMA_LOCATION only for save.

> When an instance document is serialized, if the EPackage's nsURI is
> different from the EPackage's eResource()'s URI and the
> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will automatically
> serialize an xsi:schemaLocation for the nsURI -> packageResourceURI mapping.
eResource() of the EPackage generated dynamically from an XML Schema is
null. Therefore, I thought that the EPackage has to be saved as .ecore
file as stated in the EMF book.

Thanks,
Koji



Ed Merks wrote:

> Koji,

> Comments below.


> Koji Hashimoto wrote:
>> Hi,
>>
>> I successfully generated a dynamic Ecore model from an XML Schema
>> using XSDEcoreBuilder, and created its instance using the generated
>> EPackages, by following the article below:
>> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema
>>
>>
>> How can I serialize the instance with xsi:schemaLocation?
>>
>> I set OPTION_SCHEMA_LOCATION, but it didn't work.
> You've used this option for save not just load?
>> I also tried to set "xSISchemaLocation" of the DynamicEObject
>> "DocumentRoot" by the following code, but xsi:schemaLocation did not
>> appear.
>>
>> EClass documentRootClass = (EClass)
>> ePackage.getEClassifier("DocumentRoot");
>> EObject documentRoot = EcoreUtil.create(documentRootClass);
>> EMap<String, String> map = (EMap<String, String>)
>>
documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
>>
> Using this would be simpler:

>
documentRoot.eGet(ExtendedMetaData.INSTANCE.getXSISchemaLoca tionMap(documentRoot.eClass()));


>> map.put(ePackage.getNsURI(), "sample.xsd");
>> :
>>
> I would expect the above should definitely do the trick. It does when I
> try it.
>> Do I have to serialize the Ecore model as .ecore file in advance?
> When an instance document is serialized, if the EPackage's nsURI is
> different from the EPackage's eResource()'s URI and the
> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will automatically
> serialize an xsi:schemaLocation for the nsURI -> packageResourceURI mapping.
>>
>> Thanks,
>> Koji
>>
Re: Serializing a dynamic model instance with xsi:schemaLocation [message #411310 is a reply to message #411309] Sun, 22 July 2007 09:38 Go to previous messageGo to next message
Eclipse UserFriend
Koji,

XSDEcoreBuilder.generateResources will generate resources for the Ecore
package's it creates and will set the URI of that resource to match the
package's nsURI. You could modify the resulting resources to set the
URI to be the original schema's location.


Koji Hashimoto wrote:
> Ed,
>
> Thank you for your reply.
>
>> You've used this option for save not just load?
> Yes, I have used OPTION_SCHEMA_LOCATION only for save.
>
>> When an instance document is serialized, if the EPackage's nsURI is
>> different from the EPackage's eResource()'s URI and the
>> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will
>> automatically serialize an xsi:schemaLocation for the nsURI ->
>> packageResourceURI mapping.
> eResource() of the EPackage generated dynamically from an XML Schema
> is null. Therefore, I thought that the EPackage has to be saved as
> .ecore file as stated in the EMF book.
>
> Thanks,
> Koji
>
>
>
> Ed Merks wrote:
>
>> Koji,
>
>> Comments below.
>
>
>> Koji Hashimoto wrote:
>>> Hi,
>>>
>>> I successfully generated a dynamic Ecore model from an XML Schema
>>> using XSDEcoreBuilder, and created its instance using the generated
>>> EPackages, by following the article below:
>>> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema
>>>
>>>
>>> How can I serialize the instance with xsi:schemaLocation?
>>>
>>> I set OPTION_SCHEMA_LOCATION, but it didn't work.
>> You've used this option for save not just load?
>>> I also tried to set "xSISchemaLocation" of the DynamicEObject
>>> "DocumentRoot" by the following code, but xsi:schemaLocation did not
>>> appear.
>>>
>>> EClass documentRootClass = (EClass)
>>> ePackage.getEClassifier("DocumentRoot");
>>> EObject documentRoot = EcoreUtil.create(documentRootClass);
>>> EMap<String, String> map = (EMap<String, String>)
> documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
>
>>>
>> Using this would be simpler:
>
>>
> documentRoot.eGet(ExtendedMetaData.INSTANCE.getXSISchemaLoca tionMap(documentRoot.eClass()));
>
>
>
>>> map.put(ePackage.getNsURI(), "sample.xsd");
>>> :
>>>
>> I would expect the above should definitely do the trick. It does
>> when I try it.
>>> Do I have to serialize the Ecore model as .ecore file in advance?
>> When an instance document is serialized, if the EPackage's nsURI is
>> different from the EPackage's eResource()'s URI and the
>> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will
>> automatically serialize an xsi:schemaLocation for the nsURI ->
>> packageResourceURI mapping.
>>>
>>> Thanks,
>>> Koji
>>>
>
>
Re: Serializing a dynamic model instance with xsi:schemaLocation [message #411320 is a reply to message #411310] Mon, 23 July 2007 08:30 Go to previous messageGo to next message
Eclipse UserFriend
Ed,

XSDEcoreBuilder.generateResouces() solves my problem! Thank you for
your help!

I successfully serialized an instance document with xsi:schemaLocation
by the following experimental code:

XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
Collection<Resource> resources =
xsdEcoreBuilder.generateResources(schemaURI);

EPackage ePackage = null;
for (Resource resource : resources) {
if (resource instanceof XSDResourceImpl) {
// schemaURI = resource.getURI();
} else {
ePackage = (EPackage) resource.getContents().get(0);
}
}

EObject documentRoot = EcoreUtil.create((EClass) ePackage.getEClassifier("DocumentRoot"));
ResourceSet resourceSet = ePackage.eResource().getResourceSet();
Resource resource = resourceSet.createResource(URI.createURI("platform:/resource/project/sample.xml "));
resource.getContents().add(documentRoot);

ePackage.eResource().setURI(schemaURI);
Map<Object, Object> saveOptions = new HashMap<Object, Object>(1);
saveOptions.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
resource.save(saveOptions);


By the way, the following article says "For these loaded Ecores to be
available, we need to register them to the global package registry.",
but I think this is incorrect. The snipet in the article registers
resourceSet's local package registry. Am I right?

http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema

Thanks,
Koji


Ed Merks wrote:
> Koji,
>
> XSDEcoreBuilder.generateResources will generate resources for the Ecore
> package's it creates and will set the URI of that resource to match the
> package's nsURI. You could modify the resulting resources to set the
> URI to be the original schema's location.
>
>
> Koji Hashimoto wrote:
> > Ed,
> >
> > Thank you for your reply.
> >
> >> You've used this option for save not just load?
> > Yes, I have used OPTION_SCHEMA_LOCATION only for save.
> >
> >> When an instance document is serialized, if the EPackage's nsURI is
> >> different from the EPackage's eResource()'s URI and the
> >> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will
> >> automatically serialize an xsi:schemaLocation for the nsURI ->
> >> packageResourceURI mapping.
> > eResource() of the EPackage generated dynamically from an XML Schema
> > is null. Therefore, I thought that the EPackage has to be saved as
> > .ecore file as stated in the EMF book.
> >
> > Thanks,
> > Koji
> >
> >
> >
> > Ed Merks wrote:
> >
> >> Koji,
> >
> >> Comments below.
> >
> >
> >> Koji Hashimoto wrote:
> >>> Hi,
> >>>
> >>> I successfully generated a dynamic Ecore model from an XML Schema
> >>> using XSDEcoreBuilder, and created its instance using the generated
> >>> EPackages, by following the article below:
> >>> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema
> >>>
> >>>
> >>> How can I serialize the instance with xsi:schemaLocation?
> >>>
> >>> I set OPTION_SCHEMA_LOCATION, but it didn't work.
> >> You've used this option for save not just load?
> >>> I also tried to set "xSISchemaLocation" of the DynamicEObject
> >>> "DocumentRoot" by the following code, but xsi:schemaLocation did not
> >>> appear.
> >>>
> >>> EClass documentRootClass = (EClass)
> >>> ePackage.getEClassifier("DocumentRoot");
> >>> EObject documentRoot = EcoreUtil.create(documentRootClass);
> >>> EMap<String, String> map = (EMap<String, String>)
> > documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
> >
> >>>
> >> Using this would be simpler:
> >
> >>
> > documentRoot.eGet(ExtendedMetaData.INSTANCE.getXSISchemaLoca tionMap(documentRoot.eClass()));
> >
> >
> >
> >>> map.put(ePackage.getNsURI(), "sample.xsd");
> >>> :
> >>>
> >> I would expect the above should definitely do the trick. It does
> >> when I try it.
> >>> Do I have to serialize the Ecore model as .ecore file in advance?
> >> When an instance document is serialized, if the EPackage's nsURI is
> >> different from the EPackage's eResource()'s URI and the
> >> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will
> >> automatically serialize an xsi:schemaLocation for the nsURI ->
> >> packageResourceURI mapping.
> >>>
> >>> Thanks,
> >>> Koji
> >>>
> >
> >
Re: Serializing a dynamic model instance with xsi:schemaLocation [message #411321 is a reply to message #411320] Mon, 23 July 2007 08:34 Go to previous message
Eclipse UserFriend
Koji,

Yes the article registers them in the local package registry of the
resource set, so I've modified it to not refer to the global one.
Thanks. :-)


Koji Hashimoto wrote:
> Ed,
>
> XSDEcoreBuilder.generateResouces() solves my problem! Thank you for
> your help!
>
> I successfully serialized an instance document with xsi:schemaLocation
> by the following experimental code:
>
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
> Collection<Resource> resources =
> xsdEcoreBuilder.generateResources(schemaURI);
>
> EPackage ePackage = null;
> for (Resource resource : resources) {
> if (resource instanceof XSDResourceImpl) {
> // schemaURI = resource.getURI();
> } else {
> ePackage = (EPackage) resource.getContents().get(0);
> }
> }
>
> EObject documentRoot = EcoreUtil.create((EClass) ePackage.getEClassifier("DocumentRoot"));
> ResourceSet resourceSet = ePackage.eResource().getResourceSet();
> Resource resource = resourceSet.createResource(URI.createURI("platform:/resource/project/sample.xml "));
> resource.getContents().add(documentRoot);
>
> ePackage.eResource().setURI(schemaURI);
> Map<Object, Object> saveOptions = new HashMap<Object, Object>(1);
> saveOptions.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
> resource.save(saveOptions);
>
>
> By the way, the following article says "For these loaded Ecores to be
> available, we need to register them to the global package registry.",
> but I think this is incorrect. The snipet in the article registers
> resourceSet's local package registry. Am I right?
>
> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema
>
> Thanks,
> Koji
>
>
> Ed Merks wrote:
>
>> Koji,
>>
>> XSDEcoreBuilder.generateResources will generate resources for the Ecore
>> package's it creates and will set the URI of that resource to match the
>> package's nsURI. You could modify the resulting resources to set the
>> URI to be the original schema's location.
>>
>>
>> Koji Hashimoto wrote:
>>
>>> Ed,
>>>
>>> Thank you for your reply.
>>>
>>>
>>>> You've used this option for save not just load?
>>>>
>>> Yes, I have used OPTION_SCHEMA_LOCATION only for save.
>>>
>>>
>>>> When an instance document is serialized, if the EPackage's nsURI is
>>>> different from the EPackage's eResource()'s URI and the
>>>> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will
>>>> automatically serialize an xsi:schemaLocation for the nsURI ->
>>>> packageResourceURI mapping.
>>>>
>>> eResource() of the EPackage generated dynamically from an XML Schema
>>> is null. Therefore, I thought that the EPackage has to be saved as
>>> .ecore file as stated in the EMF book.
>>>
>>> Thanks,
>>> Koji
>>>
>>>
>>>
>>> Ed Merks wrote:
>>>
>>>
>>>> Koji,
>>>>
>>>> Comments below.
>>>>
>>>
>>>> Koji Hashimoto wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I successfully generated a dynamic Ecore model from an XML Schema
>>>>> using XSDEcoreBuilder, and created its instance using the generated
>>>>> EPackages, by following the article below:
>>>>> http://wiki.eclipse.org/index.php/Generating_Dynamic_Ecore_f rom_XML_Schema
>>>>>
>>>>>
>>>>> How can I serialize the instance with xsi:schemaLocation?
>>>>>
>>>>> I set OPTION_SCHEMA_LOCATION, but it didn't work.
>>>>>
>>>> You've used this option for save not just load?
>>>>
>>>>> I also tried to set "xSISchemaLocation" of the DynamicEObject
>>>>> "DocumentRoot" by the following code, but xsi:schemaLocation did not
>>>>> appear.
>>>>>
>>>>> EClass documentRootClass = (EClass)
>>>>> ePackage.getEClassifier("DocumentRoot");
>>>>> EObject documentRoot = EcoreUtil.create(documentRootClass);
>>>>> EMap<String, String> map = (EMap<String, String>)
>>>>>
>>> documentRoot.eGet(documentRootClass.getEStructuralFeature("xSISchemaLocation "));
>>>
>>>
>>>> Using this would be simpler:
>>>>
>>>>
>>>>
>>> documentRoot.eGet(ExtendedMetaData.INSTANCE.getXSISchemaLoca tionMap(documentRoot.eClass()));
>>>
>>>
>>>
>>>
>>>>> map.put(ePackage.getNsURI(), "sample.xsd");
>>>>> :
>>>>>
>>>>>
>>>> I would expect the above should definitely do the trick. It does
>>>> when I try it.
>>>>
>>>>> Do I have to serialize the Ecore model as .ecore file in advance?
>>>>>
>>>> When an instance document is serialized, if the EPackage's nsURI is
>>>> different from the EPackage's eResource()'s URI and the
>>>> OPTION_SCHEMA_LOCATION is mapped to Boolean.TRUE, it will
>>>> automatically serialize an xsi:schemaLocation for the nsURI ->
>>>> packageResourceURI mapping.
>>>>
>>>>> Thanks,
>>>>> Koji
>>>>>
>>>>>
>>>
Previous Topic:Implementing ITableItemLableProvider breaks IItemLabelProvider impl
Next Topic:Swapping EStores
Goto Forum:
  


Current Time: Tue Oct 28 13:24:42 EDT 2025

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

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

Back to the top