Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Instantiating Ecore Model with arbitrary XML
Instantiating Ecore Model with arbitrary XML [message #1269894] Wed, 12 March 2014 09:42 Go to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
I have an XML file along with its schema.
I have created an EMF project with Ecore generated from XML Schema (XSD). The ecore is generated successfully from the schema file. Now i want to use the XML file to instantiate the generated Ecore model.

Is it possible to do that?
Re: Instantiating Ecore Model with arbitrary XML [message #1269946 is a reply to message #1269894] Wed, 12 March 2014 10:41 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33147
Registered: July 2009
Senior Member
Invoke Generate Test Code and look at the generated XyzExample. Of
course the generated editor does all this as well.

On 12/03/2014 10:42 AM, ModelGeek Mising name wrote:
> I have an XML file along with its schema.
> I have created an EMF project with Ecore generated from XML Schema
> (XSD). The ecore is generated successfully from the schema file. Now i
> want to use the XML file to instantiate the generated Ecore model.
>
> Is it possible to do that?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Instantiating Ecore Model with arbitrary XML [message #1270010 is a reply to message #1269946] Wed, 12 March 2014 11:58 Go to previous messageGo to next message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
The XML i want to use for instantiation is not XMI... it is ordinary XML but it's schema file is used to generate the Ecore.

I am able to generate Ecore successfully using following code.

XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
ResourceSet resourceSet = new ResourceSetImpl();
Collection eCorePackages = xsdEcoreBuilder.generate(URI.createFileURI("C://my.xsd"));

for (Iterator iter = eCorePackages.iterator(); iter.hasNext()Wink {
EPackage element = (EPackage) iter.next();
resourceSet.getPackageRegistry().put(element.getNsURI(), element);
}

but when i load the ordinary XML(not in XMI format) using
HashMap options = new HashMap();
options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
Resource resource = resourceSet.createResource(URI.createFileURI("C://my.xml"));
try {
resource.load(options);// here i get the exception
} catch (IOException e) {
e.printStackTrace();
}

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package with uri 'null' not found. (file:/C:/my.xml, 1, 30)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLLoadImpl.java:77)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:185)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:253)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)

any idea?

Regards,
Re: Instantiating Ecore Model with arbitrary XML [message #1270111 is a reply to message #1270010] Wed, 12 March 2014 14:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33147
Registered: July 2009
Senior Member
Comments below.

On 12/03/2014 12:58 PM, ModelGeek Mising name wrote:
> The XML i want to use for instantiation is not XMI...
No one suggested you use XMI.
> it is ordinary XML but it's schema file is used to generate the Ecore.
>
> I am able to generate Ecore successfully using following code.
Oh, you're doing this dynamically. That wasn't clear.
>
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
Have a look at the different constructors. Try storing new
BasicExtendedMetaData(new EPackageRegistryImpl()) in a variable, passing
it to the other constructor and using that extended metadata instance as
the value for XMLResource.OPTION_EXTENDED_META_DATA (to ensure that
there package corresponding to the non-namespace schema is found). You
could have a look at
org.eclipse.emf.ecore.xmi.impl.GenericXMLResourceImpl which does this
same kind of thing on the fly...
> ResourceSet resourceSet = new ResourceSetImpl();
> Collection eCorePackages =
> xsdEcoreBuilder.generate(URI.createFileURI("C://my.xsd"));
>
> for (Iterator iter = eCorePackages.iterator(); iter.hasNext();) {
> EPackage element = (EPackage) iter.next();
> resourceSet.getPackageRegistry().put(element.getNsURI(), element);
> }
>
> but when i load the ordinary XML(not in XMI format) using HashMap
> options = new HashMap();
> options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE);
> Resource resource =
> resourceSet.createResource(URI.createFileURI("C://my.xml"));
> try {
> resource.load(options);// here i get the exception
> } catch (IOException e) {
> e.printStackTrace();
> }
>
> org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Package
> with uri 'null' not found. (file:/C:/my.xml, 1, 30)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLLoadImpl.java:77)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:185)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:253)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1297)
>
> any idea?
>
> Regards,


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Instantiating Ecore Model with arbitrary XML [message #1270159 is a reply to message #1270111] Wed, 12 March 2014 15:12 Go to previous message
ModelGeek Mising name is currently offline ModelGeek Mising nameFriend
Messages: 550
Registered: June 2011
Senior Member
Thanks you very much!

It worked perfectly.
Previous Topic:CDO custom id as CDOid
Next Topic:Online docs?
Goto Forum:
  


Current Time: Tue May 14 13:57:15 GMT 2024

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

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

Back to the top