Skip to main content



      Home
Home » Modeling » EMF » Load EObject from xs:any or FeatureMap(Is it possible to read arbitrary EObject defined as xs:any in XML?)
Load EObject from xs:any or FeatureMap [message #1859374] Fri, 02 June 2023 16:39 Go to next message
Eclipse UserFriend
Hello Everyone,
I have XSD where the following complexType is defined:

<xs:complexType name="appinfo" mixed="true">
		<xs:sequence minOccurs="0" maxOccurs="unbounded">
			<xs:any processContents="lax"/>
		</xs:sequence>
</xs:complexType>


I also have the following XML that conforms the schema:

<root xmlns:pkga="com.example.packagea" xmlns:nsx="http://example.com/namespacex">
  <appinfo>
      <pkga:myClass foo="bar"/>
  </appinfo>
  <appinfo>
      <nsx:foreignElement attr="value"/>
  </appinfo>
</root>


I have model of main model package, which Root and Appinfo classes belong to, and the model of com.example.packagea package and MyClass class. There is no model for another NS with prefix "nsx".

Generated Appinfo class's content is defined as FeatureMap. I understand why (because of xs:any), but I'm wondering if there is any alternative approach, which allows me to somehow load MyClass from one of those Appinfos, if I have registered the package com.example.packagea. Can you please advise? So that I can write something like
MyClass myObj = (MyClass) appinfo.getContent().get(0);

Another requirement is that any other unknown namespaces like "nsx" in the example, should behave like now, i.e. loaded into FeatureMaps.

What I'm using now is that I traverse the FeatureMap, detecting content namespace, finding corresponding EPackage and EClass, creating the instance and populating the features one by one. It works for simple things like foo="bar" attribute, but I want to be able to load more complex objects.

If only I can take <appinfo> content like XML string or DOM model, and load EMF Resource from it, but I was unable to find a way to do it. Or maybe there is some existing utility which can create EObject from the FeatureMap?

Do you have any idea on how to solve this task? Thanks in advance!
Re: Load EObject from xs:any or FeatureMap [message #1859378 is a reply to message #1859374] Sat, 03 June 2023 01:30 Go to previous messageGo to next message
Eclipse UserFriend
Keep in mind that pkga:myClass is a reference to an element. If the model for com.example.packagea defines an element with that name, then an object of that element's (complex) type will be created. So the model for com.example.package must have a DocumentRoot EClass with an EReference named myClass all with analogous extended metadata annotations to what you see for the element named "root" from other model. Then it should just work..
Re: Load EObject from xs:any or FeatureMap [message #1859397 is a reply to message #1859378] Mon, 05 June 2023 06:37 Go to previous messageGo to next message
Eclipse UserFriend
Hello Ed,

Thank you for the answer!

UPD. Solved, please see my next post below.

It's still unclear to me what exactly should work in that case. I did as you suggested, created DocumentRoot EClass in "pkga" model and configured as needed. But I still have no API in Appinfo class to get arbitrary EObject, only the FeatureMap which still contain just EReference of type EObject, named "myClass" and having value of AnyType, not MyClass.

Should XML resource loader from EMF care about registered EPackages when it loads the elements which are xs:any according to the schema?

And another question, do I have to have XSD and Genmodel for "pkga" model, or ecore with extended metadata should be enough?

[Updated on: Mon, 05 June 2023 08:07] by Moderator

Re: Load EObject from xs:any or FeatureMap [message #1859398 is a reply to message #1859397] Mon, 05 June 2023 08:07 Go to previous messageGo to next message
Eclipse UserFriend
The Ecore with the right extended metadata is enough; if you're writing it by hand then it's really easy to get it wrong or miss some important detail. Better to generate a model from a sample *xsd (with an element with the right name and with a complex type like your EClass) to be sure you have exactly every detail correct.

Of course you provide next to no context or much in the way of clues. So I don't know if you are using the right options for loading (as in the generated resource factory). You're using the generated XyzResourceFactoryImpl to create the resource that's being loaded?
Re: Load EObject from xs:any or FeatureMap [message #1859399 is a reply to message #1859397] Mon, 05 June 2023 08:10 Go to previous messageGo to next message
Eclipse UserFriend
Hello Ed,

Sorry about my previous post, the only reason why it wasn't working is that I changed only ecore but forgot to regenerate classes... Had do debug XML Loader to notice this.
Now it works like a charm! Thank you a lot for your suggestions about DocumentRoot, this was very helpful to know!
Re: Load EObject from xs:any or FeatureMap [message #1859402 is a reply to message #1859399] Mon, 05 June 2023 09:44 Go to previous message
Eclipse UserFriend
That's rather frustrating, but all is well that ends well!
Previous Topic:CDO and H2 compatibility
Next Topic:EMF build to maven central issue ?
Goto Forum:
  


Current Time: Wed Apr 30 05:51:46 EDT 2025

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

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

Back to the top