Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Customize XML serialization with EMF
Customize XML serialization with EMF [message #953553] Mon, 22 October 2012 10:43 Go to next message
Philippe Favrais is currently offline Philippe Favrais
Messages: 16
Registered: April 2011
Junior Member
Hello
I have done an emf an ecore model able to load XML file and its instance

Here is a instance of my have a xml file as below:

Here is my model instance with the default EMF serialization
<subContainer LocalName="ADC_SETTINGS_UNIT1">
<propertie xsi:type="swp_xsd_metamodel:Parameter" LocalName="AdcClockOff" Value="No"/>
<propertie xsi:type="swp_xsd_metamodel:Parameter" LocalName="AdcOverWrite" Value="No"/>

Here is the XML serialization that i would like to get
<ADC_SETTINGS_UNIT1 AdcClockOff="No" AdcOverWrite="No"/>

How could should i proceed to tell EMF to save and load my EMF with such XML file?

I tried to use the ExtendedMetadata but did not find a way to tell EMF to replace "subContainer" by the value of "LocalName". Is it possible with extended metadata of another mechanism ?

Thank you
Philippe
Re: Customize XML serialization with EMF [message #953636 is a reply to message #953553] Mon, 22 October 2012 12:06 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26086
Registered: July 2009
Senior Member
Philippe,

Comments below.

On 22/10/2012 12:43 PM, Philippe Favrais wrote:
> Hello
> I have done an emf an ecore model able to load XML file and its instance
>
> Here is a instance of my have a xml file as below:
>
> Here is my model instance with the default EMF serialization
> <subContainer LocalName="ADC_SETTINGS_UNIT1">
> <propertie xsi:type="swp_xsd_metamodel:Parameter"
> LocalName="AdcClockOff" Value="No"/>
> <propertie xsi:type="swp_xsd_metamodel:Parameter"
> LocalName="AdcOverWrite" Value="No"/>
>
> Here is the XML serialization that i would like to get
> <ADC_SETTINGS_UNIT1 AdcClockOff="No" AdcOverWrite="No"/>
>
> How could should i proceed to tell EMF to save and load my EMF with
> such XML file?
No, that's not likely to happen. In the end, all the names used for
elements and attributes come from the model, not from the instance data.
> I tried to use the ExtendedMetadata but did not find a way to tell EMF
> to replace "subContainer" by the value of "LocalName". Is it possible
> with extended metadata of another mechanism ?
You'll need to write your own serializer and deserializer for something
like this.
>
> Thank you Philippe
Re: Customize XML serialization with EMF [message #953662 is a reply to message #953553] Mon, 22 October 2012 12:30 Go to previous messageGo to next message
Martin Weiss is currently offline Martin Weiss
Messages: 9
Registered: July 2012
Junior Member
Hi Philippe,

if I understand your question correctly your model doesn't get serialized as you expect.
Are you sure that your model represents the desired XML format?
Re: Customize XML serialization with EMF [message #953733 is a reply to message #953662] Mon, 22 October 2012 13:45 Go to previous messageGo to next message
Philippe Favrais is currently offline Philippe Favrais
Messages: 16
Registered: April 2011
Junior Member
Ed,
In fact, i have many XML files which match all different XSD files.
Here is one example of XML file
<ADC_SETTINGS_UNIT1 AdcClockOff="No" AdcOverWrite="No"/>
and the corresponding XSD :
<xs:element name="ADC_SETTINGS_UNIT1">
<xs:attribute name="AdcClockOff" type="Type_Boolean" use="optional" default="No"/>
<xs:attribute name="AdcOverWrite" type="Type_Boolean" use="optional" default="No"/>


As i have many different XML and different XSD, my intention is to load all my different XML in a same Emf MetaModel. (Below, i copied below my ecore MetaModel)
So I have defined a mapping between my XML/XSD and my Emf MetaModel, now i need to implement a specific load and save so that my XML files get loaded/saved in Emf.

The Mapping is :
An XML Element is a Container or SubContainer (in my Ecore) where LocalName contains the XML Element Name
An XML Attribute is a Parameter (in my Ecore) where LocalName is the XML attribute Name and Value is the XML attribute value

Philippe
Re: Customize XML serialization with EMF [message #953777 is a reply to message #953733] Mon, 22 October 2012 14:19 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26086
Registered: July 2009
Senior Member
Comments below.


On 22/10/2012 3:45 PM, Philippe Favrais wrote:
> Ed,
> In fact, i have many XML files which match all different XSD files.
> Here is one example of XML file
> <ADC_SETTINGS_UNIT1 AdcClockOff="No" AdcOverWrite="No"/>
> and the corresponding XSD :
> <xs:element name="ADC_SETTINGS_UNIT1">
> <xs:attribute name="AdcClockOff" type="Type_Boolean" use="optional" default="No"/>
> <xs:attribute name="AdcOverWrite" type="Type_Boolean" use="optional" default="No"/>
>
>
> As i have many different XML and different XSD, my intention is to load all my different XML in a same Emf MetaModel. (Below, i copied below my ecore MetaModel)
> So I have defined a mapping between my XML/XSD and my Emf MetaModel, now i need to implement a specific load and save so that my XML files get loaded/saved in Emf.
As I said, you'll have to do that by hand.

In the end, this all seems kind of pointless though. What value do you
get from your approach? Given you have a bunch of different models for
all the different schemas, and you load some resource that
automatically uses the right model, you can always traverse that data
generically via EMF's reflective APIs, e.g., Resource.getAllContents().
Also, things like ExtendedMetaData.INSTANCE.getName(eObject.eClass())
and ExtendedMetaData.INSTANCE.getName(eObject.eContainmentFeature())
will give you the XML names in the instance data. After all, all the
information in the original XML is recoverable, otherwise EMF wouldn't
be able to serialize it back out to conforming XML.
>
> The Mapping is :
> An XML Element is a Container or SubContainer (in my Ecore) where LocalName contains the XML Element Name
> An XML Attribute is a Parameter (in my Ecore) where LocalName is the XML attribute Name and Value is the XML attribute value
Why do this?
>
> Philippe
Re: Customize XML serialization with EMF [message #954650 is a reply to message #953777] Tue, 23 October 2012 06:21 Go to previous messageGo to next message
Philippe Favrais is currently offline Philippe Favrais
Messages: 16
Registered: April 2011
Junior Member
Hello,
is there somewhere available an sample on how i should proceed to overload the serialization/deserialization of an Emf Model (generated from my ecore) ?
thank you
Philippe
Re: Customize XML serialization with EMF [message #954683 is a reply to message #954650] Tue, 23 October 2012 07:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26086
Registered: July 2009
Senior Member
Philippe,

You can create a derived ResourceImpl and override doLoad and doSave.
You can look at the type hierarchy of that class to see how other
implementations specialize it. (I still don't see the point.)

On 23/10/2012 8:21 AM, Philippe Favrais wrote:
> Hello,
> is there somewhere available an sample on how i should proceed to
> overload the serialization/deserialization of an Emf Model (generated
> from my ecore) ?
> thank you
> Philippe
Re: Customize XML serialization with EMF [message #954783 is a reply to message #954683] Tue, 23 October 2012 08:43 Go to previous messageGo to next message
Philippe Favrais is currently offline Philippe Favrais
Messages: 16
Registered: April 2011
Junior Member
I want to load my XML different files into my generic Ecore model because in the end i want to be able to compare them (with EMF compare: i have already implemented my own match engine). Do you think that it could be done on a easier way ?
Philippe

Re: Customize XML serialization with EMF [message #954801 is a reply to message #954783] Tue, 23 October 2012 09:00 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26086
Registered: July 2009
Senior Member
Philippe,

You want to compare different types of models? Or just compare
different instances of the same model?

On 23/10/2012 10:43 AM, Philippe Favrais wrote:
> I want to load my XML different files into my generic Ecore model
> because in the end i want to be able to compare them (with EMF
> compare: i have already implemented my own match engine). Do you think
> that it could be done on a easier way ?
> Philippe
>
>
Re: Customize XML serialization with EMF [message #955017 is a reply to message #954801] Tue, 23 October 2012 12:39 Go to previous messageGo to next message
Philippe Favrais is currently offline Philippe Favrais
Messages: 16
Registered: April 2011
Junior Member
I need to compare XML files which correspond to the same XSD.
As i have many different XSD files (depending on what i model), i need a generic tool able to compare all kinds of XML files (corresponding to the same XSD)
e.g.:
i need to compare Adc_0.xml against Adc_1.xml which both match Adc.Xsd
i need to compare Pwm_0.xml against Pwm_1.xml which both match Pwm.Xsd
i need to compare Irq_0.xml against Irq_1.xml which both match Irq.Xsd
....
All my XSD are done with the same simple "design rule":
when an element is not a singleton (max cardinality > 1), it has an attribute @Name which allow me to identify it uniquely. In that case, i compare the element having the same element name and the same attribute @Name value (independently of their order in the XML file)
when an element is a singleton, i use only the element name to match and compare the element
e.g:
<Root>
<Channel Name="abc"/>
<Channel Name="def"/> --> (<Channel> is not a singleton because it has an attribute @Name: so in my compare engine, i compare the element if they haevthe same element name and atribute name)
<Properties Default="Off" Clock="8"> --> (<Properties> is a singleton because it do not have @Name: so it will be compared against another element <Properties/>)
</Root>

So far, to experiment Emf Compare, i implemented a transformer to convert my XML(compliant to XSD) files to XMI(Ecore) and then in a 2nd step, i compare these Ecore model instances.
For the next step i would like to automatically transfrom my xml file while loading and saving from the Compare tool.

Do you think this approach is the rigth one ?
Philippe
Re: Customize XML serialization with EMF [message #955927 is a reply to message #955017] Wed, 24 October 2012 05:30 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26086
Registered: July 2009
Senior Member
Philippe,

Comments below.

On 23/10/2012 2:39 PM, Philippe Favrais wrote:
> I need to compare XML files which correspond to the same XSD. As i
> have many different XSD files (depending on what i model), i need a
> generic tool able to compare all kinds of XML files (corresponding to
> the same XSD)
EMF compare and EMF diff both work generically.
> e.g.: i need to compare Adc_0.xml against Adc_1.xml which both match
> Adc.Xsd
> i need to compare Pwm_0.xml against Pwm_1.xml which both match Pwm.Xsd
> i need to compare Irq_0.xml against Irq_1.xml which both match Irq.Xsd
> ...
> All my XSD are done with the same simple "design rule": when an
> element is not a singleton (max cardinality > 1), it has an attribute
> @Name which allow me to identify it uniquely. In that case, i compare
> the element having the same element name and the same attribute @Name
> value (independently of their order in the XML file)
> when an element is a singleton, i use only the element name to match
> and compare the element
> e.g:
> <Root>
> <Channel Name="abc"/>
> <Channel Name="def"/> --> (<Channel> is not a singleton because it has
> an attribute @Name: so in my compare engine, i compare the element if
> they haevthe same element name and atribute name)
> <Properties Default="Off" Clock="8"> --> (<Properties> is a singleton
> because it do not have @Name: so it will be compared against another
> element <Properties/>)
> </Root>
>
> So far, to experiment Emf Compare, i implemented a transformer to
> convert my XML(compliant to XSD) files to XMI(Ecore) and then in a 2nd
> step, i compare these Ecore model instances.
> For the next step i would like to automatically transfrom my xml file
> while loading and saving from the Compare tool.
>
> Do you think this approach is the rigth one ?
It sounds unnecessary. The rules you describe sound like something you
could characterize generically. EMF compare has mechanisms for
determining identity and you could specialize those rules in a generic
way. You might, for example, put annotations on the Ecore models you
produce from these schemas (i.e., via XML Schema annotations that are
mapped to Ecore) to mark the features (@Name) to be used for
establishing identity. The element name doesn't matter because it will
only compare objects contained by the same feature and that feature is
determined by the element name.
> Philippe
Previous Topic:[CDO/Hibernate] Export and Import
Next Topic:[CDO/Hibernate] Exporting Hibernate Mapping
Goto Forum:
  


Current Time: Tue Sep 30 10:11:14 GMT 2014

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

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