Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Import XML model to EMF(Trying to import XSD schema to ECORE and instantiate the model from XML file)
Import XML model to EMF [message #1450267] Wed, 22 October 2014 09:03 Go to next message
Bogdan Panait is currently offline Bogdan PanaitFriend
Messages: 2
Registered: October 2014
Location: Bucharest
Junior Member
Hi,

So, as the topic says, I'm trying to import a model programmatically to EMF. I'm doing this by loading and registering the XSD first, after which I'm trying to populate the model with the information provided in my XML file.

Here is the code I came up with so far:
*************************************** CODE ***************************************
/*
* Load the XSD
*/
XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
ResourceSet resourceSet = new ResourceSetImpl();
/*
* Register a factory for the XSD resource
*/
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd",
new XSDResourceFactoryImpl());
Collection <EObject> eCorePackages = xsdEcoreBuilder.generate(
URI.createFileURI("D:\\Model.xsd"));
/*
* Register the packages to the package registry
*/
for (EObject eCorePackage:eCorePackages) {
resourceSet.getPackageRegistry().put(
((EPackage) eCorePackage).getNsURI(),
(EPackage) eCorePackage);
}
/*
* Load the XML
*/
URI xmlUri = URI.createFileURI("D:\\Model.xml");
/*
* Register a factory for the XML resource
*/
Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xml",
new XMLResourceFactoryImpl());

Map<Object, Object> options = resourceSet.getLoadOptions();

options.put(XMLResource.OPTION_EXTENDED_META_DATA,
ExtendedMetaData.INSTANCE);
options.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE);

Resource resource = resourceSet.getResource(xmlUri, true);

*************************************** CODE ***************************************
This generates the following error stack:
*************************************** STACK ***************************************
Exception in thread "main" org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'file:/D:/Model.xsd' not found. (file:/D:/Model.xml, 2, 204)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406)
at com.xml2ecore.LoadModel.loadXMLModel(LoadModel.java:68)
at com.xml2ecore.Main.main(Main.java:24)
Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri 'file:/D:/Model.xsd' not found. (file:/D:/Model.xml, 2, 204)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2586)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processSchemaLocations(XMLHandler.java:1725)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.handleTopLocations(XMLHandler.java:1743)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1291)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1463)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:1014)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:996)
at org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:707)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown Source)
at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)
at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:240)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1505)
at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1284)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
... 3 more

*************************************** STACK ***************************************

Can someone please help me on this, or point me to some useful information?
Many thanks to all the people keeping this forum alive Smile

[Updated on: Wed, 22 October 2014 13:37]

Report message to a moderator

Re: Import XML model to EMF [message #1450472 is a reply to message #1450267] Wed, 22 October 2014 13:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 30541
Registered: July 2009
Senior Member
Bogdan,

What does the XML look like? Specifically I'm interested in the root
element, its xmlns declarations, and any xsi:schemaLocation
information. From the information provided, it seems it wants to find
something for the namespace file:/D:/Model.xsd but I doubt that's really
the target namespace of your schema. So I need to know, what is the
target namespace of your schema and how is it being used in the
instance XML.


On 22/10/2014 3:03 PM, Bogdan Panait wrote:
> Hi,
>
> So, as the topic says, I'm trying to import a model programmatically
> to EMF. I'm doing this by loading and registering the XSD first, after
> which I'm trying to populate the model with the information provided
> in my XML file.
>
> Here is the code I came up with so far:
> *************************************** CODE
> ***************************************
> /*
> * Load the XSD
> */
> XSDEcoreBuilder xsdEcoreBuilder = new XSDEcoreBuilder();
> ResourceSet resourceSet = new ResourceSetImpl();
> /*
> * Register a factory for the XSD resource
> */
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd",
> new XSDResourceFactoryImpl());
> Collection <EObject> eCorePackages = xsdEcoreBuilder.generate(
> URI.createFileURI("D:\\Model.xsd"));
> /*
> * Register the packages to the package registry
> */
> for (EObject eCorePackage:eCorePackages) {
> resourceSet.getPackageRegistry().put(
> ((EPackage) eCorePackage).getNsURI(),
> (EPackage) eCorePackage);
> }
> /*
> * Load the XML
> */
> URI xmlUri = URI.createFileURI("D:\\Model.xml");
> /*
> * Register a factory for the XML resource
> */
> Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xml",
> new XMLResourceFactoryImpl());
>
> Map<Object, Object> options = resourceSet.getLoadOptions();
>
> options.put(XMLResource.OPTION_EXTENDED_META_DATA,
> ExtendedMetaData.INSTANCE);
> options.put(XMLResource.OPTION_USE_LEXICAL_HANDLER, Boolean.TRUE);
>
> Resource resource = resourceSet.getResource(xmlUri, true);
>
> *************************************** CODE
> ***************************************
> This generates the following error stack:
> *************************************** STACK
> ***************************************
> Exception in thread "main"
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException:
> org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package with uri
> 'file:/D:/Model.xsd' not found. (file:/D:/Model.xml, 2, 204)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getResource(ResourceSetImpl.java:406)
> at com.xml2ecore.LoadModel.loadXMLModel(LoadModel.java:68)
> at com.xml2ecore.Main.main(Main.java:24)
> Caused by: org.eclipse.emf.ecore.xmi.PackageNotFoundException: Package
> with uri 'file:/D:/Model.xsd' not found. (file:/D:/Model.xml, 2, 204)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.getPackageForURI(XMLHandler.java:2586)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processSchemaLocations(XMLHandler.java:1725)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.handleTopLocations(XMLHandler.java:1743)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createObjectByType(XMLHandler.java:1291)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.createTopObject(XMLHandler.java:1463)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.processElement(XMLHandler.java:1014)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:996)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLHandler.startElement(XMLHandler.java:707)
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.startElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$ContentDriver.scanRootElementHook(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown
> Source)
> at
> com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(Unknown
> Source)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:175)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:240)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1505)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1284)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
> at
> org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
> ... 3 more
>
> *************************************** STACK
> ***************************************
>
> Can someone please help me on this, or point me to some useful
> information?
> Many thanks to all the people keeping this forum alive :)
Re: Import XML model to EMF [message #1450518 is a reply to message #1450472] Wed, 22 October 2014 14:24 Go to previous messageGo to next message
Bogdan Panait is currently offline Bogdan PanaitFriend
Messages: 2
Registered: October 2014
Location: Bucharest
Junior Member
Hi Ed,

Thank you for the quick reply, it's really appreciated.

Here is the XML information:

<?xml version="1.0" encoding="utf-8"?>
<Model xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance" FixedStep="auto" Name="Model2" SolverMode="Auto" SolverName="ode45" StartTime="0.0" StopTime="10.0" xsi:noNamespaceSchemaLocation="Model.xsd">
...
</Model>

And the XSD information:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http ://www.w3.org/2001/XMLSchema">
<xsd:element name="Model" type="Model"></xsd:element>
<!-- Many abstract type declarations -->
<xsd:complexType ...>
..
..
..
</xsd:complexType>
</xsd:schema>

Thanks again for taking the time to look into this! Smile, I'm new to EMF technology and trying to get an understanding.
Re: Import XML model to EMF [message #1450604 is a reply to message #1450518] Wed, 22 October 2014 16:40 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 30541
Registered: July 2009
Senior Member
Bogdan,

Comments below.


On 22/10/2014 4:24 PM, Bogdan Panait wrote:
> Hi Ed,
>
> Thank you for the quick reply, it's really appreciated.
>
> Here is the XML information:
>
> <?xml version="1.0" encoding="utf-8"?>
> <Model xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
> FixedStep="auto" Name="Model2" SolverMode="Auto" SolverName="ode45"
> StartTime="0.0" StopTime="10.0"
> xsi:noNamespaceSchemaLocation="Model.xsd">
I see so there is no target namespace and the schema location is
relative so it resolves to the absolute URI you showed. In this loop:

for (EObject eCorePackage:eCorePackages) {
resourceSet.getPackageRegistry().put(
((EPackage) eCorePackage).getNsURI(),
(EPackage) eCorePackage);
}

what did it place into the registry? Isn't the nsURI of this generated
package exactly file:/D:/Model.xsd?

Probably the problem is this:


options.put(XMLResource.OPTION_EXTENDED_META_DATA,
ExtendedMetaData.INSTANCE);

Here you're telling it to use the global registry so it won't even look
in the resource set's package registry. Try Boolean.TRUE as the value
so that it create ones that wraps the resource set's package registry.
> ...
> </Model>
>
> And the XSD information:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsd:schema elementFormDefault="qualified" xmlns:xsd="http
> ://www.w3.org/2001/XMLSchema">
> <xsd:element name="Model" type="Model"></xsd:element>
> <!-- Many abstract type declarations -->
> <xsd:complexType ...>
> ..
> ..
> ..
> </xsd:complexType>
> </xsd:schema>
>
> Thanks again for taking the time to look into this! :), I'm new to EMF
> technology and trying to get an understanding.
Previous Topic:Add duplicated elements to EList
Next Topic:Customize XML deserialization so old project can be loaded into modified model
Goto Forum:
  


Current Time: Tue Oct 15 04:19:40 GMT 2019

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

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

Back to the top