Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » XML serialization
XML serialization [message #1764385] Mon, 29 May 2017 10:06 Go to next message
Peter Mising name is currently offline Peter Mising nameFriend
Messages: 95
Registered: July 2009
Member
Hello, I have a little problem with the XML serialization.

I want to deserialize and serialize the following xml.
  <spec>
        <customerAddress>asd</customerAddress>
        <OrderIDREF>k1</OrderIDREF>
        <OrderIDREFS>k1 k2</OrderIDREFS>
  </spec>


The orders that are referenced have the id (eType="ecore:EDataType http://www.eclipse.org/emf/2003/XMLType#//ID" iD="true")

The orderIDREF reference works fine with this annotation
  <eAnnotations source="http:///org/eclipse/emf/ecore/util/ExtendedMetaData">
        <details key="kind" value="element"/>
        <details key="name" value="OrderIDREF"/>
        <details key="namespace" value="##targetNamespace"/>
      </eAnnotations>


The issue is the orderIDREFS reference. I figured out that kind element only works with single references so i tried group and elementWildcard. They also work when loading the xml but when saving it, it always moves the entry into the spec tag like this
<spec OrderIDREFS="k1 k2">
     <customerAddress>asd</customerAddress>
    <OrderIDREF>k1</OrderIDREF>
</spec>


When i want to load this again it fails with "Feature 'OrderIDREFS' not found"

Is there any setting that I can make to force it to leave it in its own tag?

Thank you

[Updated on: Mon, 29 May 2017 11:00]

Report message to a moderator

Re: XML serialization [message #1764390 is a reply to message #1764385] Mon, 29 May 2017 10:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
Did you try "simple" as the kind?

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: XML serialization [message #1764391 is a reply to message #1764390] Mon, 29 May 2017 10:52 Go to previous messageGo to next message
Peter Mising name is currently offline Peter Mising nameFriend
Messages: 95
Registered: July 2009
Member
With 'simple' the Feature not found exception occurs while loading the original xml.
Re: XML serialization [message #1764426 is a reply to message #1764391] Mon, 29 May 2017 15:26 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
I tried defining an XSD that uses type IDREFS but the model that comes out is ugly and includes a "holder" EClass with a feature of simple kind... So unfortunately there isn't a way to get the serialization you want without making the model ugly. If you must deal with a given XML serialization, it's probably best to use the XML Schema for it, or define an XML Schema for it, and import that into an Ecore model.

Ed Merks
Professional Support: https://www.macromodeling.com/
Re: XML serialization [message #1764430 is a reply to message #1764426] Mon, 29 May 2017 16:14 Go to previous messageGo to next message
Peter Mising name is currently offline Peter Mising nameFriend
Messages: 95
Registered: July 2009
Member
double post. sry

[Updated on: Mon, 29 May 2017 16:19]

Report message to a moderator

Re: XML serialization [message #1764432 is a reply to message #1764426] Mon, 29 May 2017 16:17 Go to previous messageGo to next message
Peter Mising name is currently offline Peter Mising nameFriend
Messages: 95
Registered: July 2009
Member
Hi Ed,
thanks for the answer.
I actually have a xml schema. But IDREFS will only be a string attribute. I thought it would be great if they are represented as eReferences with the real Type. Then the resolving is done at load time and it also checks if the string refs are valid and so on. It actual works really well.
I have a URIHandler to deresolve and resolve the uris the right way.
If i store them in seperated tags it also works with the element kind.

<spec>
   <OrderIDREFS>k1</OrderIDREFS>
   <OrderIDREFS>k2</OrderIDREFS>
 </spec>


Just the space seperated multi references in a single tag. don't work.
I thought maybe I miss something. But maybe it is not possible.
Thanks for your help.

Re: XML serialization [message #1764436 is a reply to message #1764432] Mon, 29 May 2017 16:57 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
Registered: July 2009
Senior Member
You can use IDREFS as the type but also include ecore:reference="qname" to refer to the complex type you want it to really be. That should work, though the model will include an intermediate "holder" class. E.g., this sort of works but the ecore:eOpposite are invalid because holder class holds the reference...
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.example.eclipse.org/Library"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
    xmlns:lib="http://www.example.eclipse.org/Library"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="Book">
    <xsd:sequence>
      <xsd:element name="title" type="xsd:string"/>
      <xsd:element name="pages" type="xsd:int"/>
      <xsd:element name="category" type="lib:BookCategory"/>
      <xsd:element name="author" type="xsd:IDREF"
          ecore:reference="lib:Writer" ecore:opposite="books"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="Writer">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element maxOccurs="unbounded" minOccurs="0" name="books"
          type="xsd:IDREFS" ecore:reference="lib:Book" ecore:opposite="author"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:complexType name="Library">
    <xsd:sequence>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element maxOccurs="unbounded" minOccurs="0"
          name="writers" type="lib:Writer"/>
      <xsd:element maxOccurs="unbounded" minOccurs="0"
          name="books" type="lib:Book"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="BookCategory">
    <xsd:restriction base="xsd:NCName">
      <xsd:enumeration value="Mystery"/>
      <xsd:enumeration value="ScienceFiction"/>
      <xsd:enumeration value="Biography"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>


Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Loading Xtext Resource Into EMF Editor Breaks on Save?
Next Topic:[CDO] IllegalStateException when saving custom Permission
Goto Forum:
  


Current Time: Fri Apr 19 23:54:18 GMT 2024

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

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

Back to the top