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 |
Bogdan Panait 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
[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 |
Ed Merks Messages: 33142 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 :)
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: Import XML model to EMF [message #1450604 is a reply to message #1450518] |
Wed, 22 October 2014 16:40 |
Ed Merks Messages: 33142 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.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Goto Forum:
Current Time: Fri Apr 26 15:52:14 GMT 2024
Powered by FUDForum. Page generated in 0.04141 seconds
|