Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF load resource with different namespaces (prefixes)
EMF load resource with different namespaces (prefixes) [message #657571] Thu, 03 March 2011 10:32 Go to next message
Stephan Krusche is currently offline Stephan KruscheFriend
Messages: 18
Registered: August 2010
Location: Munich
Junior Member
I have two xsd files, both in the same folder:

model.xsd

<xs:schema xmlns:model2="http://www.ns.com/model2" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" elementFormDefault="qualified" ecore:package="mytool.model.model" ecore:documentRoot="Model">
	<xs:import namespace="http://www.ns.com/model2" schemaLocation="model2.xsd"/>
	<xs:element name="attribute">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="model2:model2element"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>


model2.xsd

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" elementFormDefault="qualified" targetNamespace="http://www.ns.com/model2" xmlns:model2="http://www.ns.com/model2" ecore:package="mytool.model.model2" ecore:documentRoot="Model2">
	<xs:element name="model2element">
		<xs:complexType>
			<xs:sequence>
				<xs:element ref="model2:name" minOccurs="1" maxOccurs="unbounded"/>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
	<xs:element name="name" type="xs:string"/>
</xs:schema>


I generated two corresponding ecore files and a genmodel with that and I also generated the Java model code. So far no problems.

Now I want to load the following xml file:

modelfile.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<attribute>
	<model2:model2element>
		<model2:name>testString</model2:name>
	</model2:model2element>
</attribute>


with the following Java code:

	private void load(IFile selectedFile) 
	{
		//File modelXmlFile = new File("modelfile.xml");
		ResourceSet resourceSet = new ResourceSetImpl();
		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xml", new ModelResourceFactoryImpl());
		java.net.URI uri = selectedFile.getLocationURI();
		URI fileUri = URI.createURI(uri.toString(), true);
		XMLResource resource = null;
		try 
		{
			resource = (XMLResource) resourceSet.createResource(fileUri);
			resourceSet.getPackageRegistry().put(ModelPackage.eNS_URI, ModelPackage.eINSTANCE);
			resourceSet.getPackageRegistry().put(Model2Package.eNS_URI, Model2Package.eINSTANCE);

			resource.load(null);
		} 
		catch (Exception ex) 
		{
			ex.printStackTrace();
		}
		resourceSet.getResources().add(resource);
		Model myModel = (Model) resource.getContents().get(0);
		System.out.println(myModel);
	}


But I get the following exception:

org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Feature 'model2element' not found. (file:/D:/test/runtime-EclipseApplication/test/modelfile.xml, 3, 24)
	at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLLoadImpl.java:83)
	at org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl.java:191)
	at org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLResourceImpl.java:180)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1494)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1282)
	at mytool.ui.popup.actions.NewAction.load(NewAction.java:75)
	...


I assume that I somehow must register the prefix of the second factory, but I don't know how Sad

What is wrong?

Thank you for help!
Re: EMF load resource with different namespaces (prefixes) [message #657675 is a reply to message #657571] Thu, 03 March 2011 15:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Stephan,

Comments below.

Stephan Krusche wrote:
> I have two xsd files, both in the same folder:
>
> model.xsd
>
>
> <xs:schema xmlns:model2="http://www.ns.com/model2"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> elementFormDefault="qualified" ecore:package="mytool.model.model"
> ecore:documentRoot="Model">
> <xs:import namespace="http://www.ns.com/model2"
> schemaLocation="model2.xsd"/>
> <xs:element name="attribute">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="model2:model2element"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> </xs:schema>
>
>
> model2.xsd
>
>
> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
> elementFormDefault="qualified"
> targetNamespace="http://www.ns.com/model2"
> xmlns:model2="http://www.ns.com/model2"
> ecore:package="mytool.model.model2" ecore:documentRoot="Model2">
> <xs:element name="model2element">
> <xs:complexType>
> <xs:sequence>
> <xs:element ref="model2:name" minOccurs="1"
> maxOccurs="unbounded"/>
> </xs:sequence>
> </xs:complexType>
> </xs:element>
> <xs:element name="name" type="xs:string"/>
> </xs:schema>
>
>
> I generated two corresponding ecore files and a genmodel with that and
> I also generated the Java model code. So far no problems.
Looks like a problem to me. There's both one logical schema (there's
only one targetNamespace) split across to resources.
>
> Now I want to load the following xml file:
>
> modelfile.xml
>
>
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <attribute>
> <model2:model2element>
> <model2:name>testString</model2:name>
> </model2:model2element>
> </attribute>
>
>
> with the following Java code:
>
>
> private void load(IFile selectedFile) {
> //File modelXmlFile = new File("modelfile.xml");
> ResourceSet resourceSet = new ResourceSetImpl();
>
> resourceSet.getResourceFactoryRegistry().getExtensionToFacto ryMap().put( "xml",
> new ModelResourceFactoryImpl());
> java.net.URI uri = selectedFile.getLocationURI();
> URI fileUri = URI.createURI(uri.toString(), true);
> XMLResource resource = null;
> try {
> resource = (XMLResource) resourceSet.createResource(fileUri);
> resourceSet.getPackageRegistry().put(ModelPackage.eNS_URI,
> ModelPackage.eINSTANCE);
>
> resourceSet.getPackageRegistry().put(Model2Package.eNS_URI,
> Model2Package.eINSTANCE);
>
> resource.load(null);
> } catch (Exception ex) {
> ex.printStackTrace();
> }
> resourceSet.getResources().add(resource);
> Model myModel = (Model) resource.getContents().get(0);
> System.out.println(myModel);
> }
>
>
> But I get the following exception:
>
>
> org.eclipse.emf.ecore.resource.Resource$IOWrappedException: Feature
> 'model2element' not found.
> (file:/D:/test/runtime-EclipseApplication/test/modelfile.xml , 3, 24)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.handleErrors(XMLL oadImpl.java:83)
>
> at
> org.eclipse.emf.ecore.xmi.impl.XMLLoadImpl.load(XMLLoadImpl. java:191)
> at
> org.eclipse.emf.ecore.xmi.impl.XMLResourceImpl.doLoad(XMLRes ourceImpl.java:180)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1494)
>
> at
> org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(Resour ceImpl.java:1282)
>
> at mytool.ui.popup.actions.NewAction.load(NewAction.java:75)
> ...
>
>
> I assume that I somehow must register the prefix of the second
> factory, but I don't know how :(
>
> What is wrong?
You can't have more than one package with the same namespace. You need
to generate one package from the two schemas.
>
> Thank you for help!


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF load resource with different namespaces (prefixes) [message #658118 is a reply to message #657675] Sun, 06 March 2011 17:33 Go to previous messageGo to next message
Stephan Krusche is currently offline Stephan KruscheFriend
Messages: 18
Registered: August 2010
Location: Munich
Junior Member
Ed Merks wrote on Thu, 03 March 2011 10:52
You can't have more than one package with the same namespace. You need
to generate one package from the two schemas


How can I achieve this?

I read that xml schema can only have one namespace per schema file, so I need two schema files. When I use the New EMF Generator Model wizard and choose XML Schema in the Model Importers page, always two ecore files and two packages are generated... Is there any trick that only one package will be generated?

Thank you!
Re: EMF load resource with different namespaces (prefixes) [message #658120 is a reply to message #658118] Sun, 06 March 2011 18:03 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------020701040302030007080001
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Stephan,

Comments below.

Stephan Krusche wrote:
> Ed Merks wrote on Thu, 03 March 2011 10:52
>> You can't have more than one package with the same namespace. You need
>> to generate one package from the two schemas
>
>
> How can I achieve this?
> I read that xml schema can only have one namespace per schema file, so
> I need two schema files.
Oh, sorry I misread them as having the same target namespace. I see now
one has a target namespace and the other doesn't.
> When I use the New EMF Generator Model wizard and choose XML Schema in
> the Model Importers page, always two ecore files and two packages are
> generated... Is there any trick that only one package will be generated?
You say you want to read

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<attribute>
<model2:model2element>
<model2:name>testString</model2:name>
</model2:model2element>
</attribute>

but that's not a valid instance. I think you meant this, right?

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<attribute xmlns:model2="http://www.ns.com/model2">
<model2:model2element>
<model2:name>testString</model2:name>
</model2:model2element>
</attribute>

>
> Thank you!

--------------020701040302030007080001
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Stephan,<br>
<br>
Comments below.<br>
<br>
Stephan Krusche wrote:
<blockquote cite="mid:il0g5c$efq$1@news.eclipse.org" type="cite">Ed
Merks wrote on Thu, 03 March 2011 10:52
<br>
<blockquote type="cite">You can't have more than one package with the
same namespace. You need
<br>
to generate one package from the two schemas
<br>
</blockquote>
<br>
<br>
How can I achieve this? <br>
I read that xml schema can only have one namespace per schema file, so
I need two schema files. </blockquote>
Oh, sorry I misread them as having the same target namespace.  I see
now one has a target namespace and the other doesn't.<br>
<blockquote cite="mid:il0g5c$efq$1@news.eclipse.org" type="cite">When I
use the New EMF Generator Model wizard and choose XML Schema in the
Model Importers page, always two ecore files and two packages are
generated... Is there any trick that only one package will be
generated?
<br>
</blockquote>
You say you want to read <br>
<br>
&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
<br>
&lt;attribute&gt;
<br>
    &lt;model2:model2element&gt;
<br>
        &lt;model2:name&gt;testString&lt;/model2:name&am p;gt;
<br>
    &lt;/model2:model2element&gt;
<br>
&lt;/attribute&gt;
<br>
<br>
but that's not a valid instance.  I think you meant this, right?<br>
<blockquote>&lt;?xml version="1.0" encoding="UTF-8"
standalone="no"?&gt;
<br>
&lt;attribute xmlns:model2=<a class="moz-txt-link-rfc2396E"
href="http://www.ns.com/model2">"http://www.ns.com/model2</a>"&gt;
<br>
    &lt;model2:model2element&gt;
<br>
        &lt;model2:name&gt;testString&lt;/model2:name&am p;gt;
<br>
    &lt;/model2:model2element&gt;
<br>
&lt;/attribute&gt;
<br>
</blockquote>
<blockquote cite="mid:il0g5c$efq$1@news.eclipse.org" type="cite"><br>
Thank you!
<br>
</blockquote>
</body>
</html>

--------------020701040302030007080001--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF load resource with different namespaces (prefixes) [message #662001 is a reply to message #657571] Mon, 28 March 2011 20:04 Go to previous messageGo to next message
Stephan Krusche is currently offline Stephan KruscheFriend
Messages: 18
Registered: August 2010
Location: Munich
Junior Member
Hello Ed,

I start with the following xml:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model1element xmlns:model2="urn:/a/b/c-section">
	<model2:model2element>
		<model2:name>testString</model2:name>
	</model2:model2element>
</model1element>


This is delivered by the customer and cannot be modified. My goal is to deserialize it with EMF into Java objects to be able to use it for further M2M and M2T transformations.

In the xsd and ecore creation I am free as long as I can load the given xml.

So how must the xsd look like that I can generate valid ecore and that the deserialization into Java objects works without problems?

Thank you
Stephan

Re: EMF load resource with different namespaces (prefixes) [message #662021 is a reply to message #662001] Mon, 28 March 2011 22:30 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33113
Registered: July 2009
Senior Member
Stephan,

Write a schema to which this XML conforms and generate the Ecore model
corresponding to that. My blog shows how to make use of substitution
groups if you need them. I assume you're not just asking me to write
the schema for you...


Stephan Krusche wrote:
> Hello Ed,
>
> I start with the following xml:
>
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <model1element xmlns:model2="urn:/a/b/c-section">
> <model2:model2element>
> <model2:name>testString</model2:name>
> </model2:model2element>
> </model1element>
>
>
> This is delivered by the customer and cannot be modified. My goal is
> to deserialize it with EMF into Java objects to be able to use it for
> further M2M and M2T transformations.
>
> In the xsd and ecore creation I am free as long as I can load the
> given xml.
>
> So how must the xsd look like that I can generate valid ecore and that
> the deserialization into Java objects works without problems?
>
> Thank you
> Stephan
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:[teneo] UUID with containment causes "no String constructor" exception on retrieval
Next Topic:Creating child objects of a type from another model?
Goto Forum:
  


Current Time: Thu Mar 28 14:03:48 GMT 2024

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

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

Back to the top