Pattern for reifying XML doc?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Pattern for reifying XML doc?

stephane ducasse
Hi guys

I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
Here is an example

<group>
    <groupName>Test2Cards</groupName>
    <cards><card>
    <cardName>All Is Dust</cardName>
    <manaCost>7</manaCost>
    <convertedManaCost>7</convertedManaCost>
    <type>Tribal Sorcery — Eldrazi</type>
    <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
    <rarity>mythic</rarity>
    <cardNumber>1</cardNumber>
    <artist>Jason Felix</artist>
    <kind>STD</kind>
    <expansionId>ELDRAZI</expansionId>
</card>
<card>
    <cardName>Kird Ape</cardName>
    <manaCost>G</manaCost>
    <convertedManaCost>1</convertedManaCost>
    <type>Tribal Sorcery — Eldrazi</type>
    <cardText>get +1/+1 when you control</cardText>
    <rarity>common</rarity>
    <cardNumber>33</cardNumber>
    <artist>Zork</artist>
    <kind>STD</kind>
    <expansionId>WORLDWAKE</expansionId>
</card></cards>
</group>

I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
and map that to objects. In the future I would like to have magritte like type of first class instance variables that
perform i/o in xml or other formats because this is always the same.

stef



Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Schwab,Wilhelm K
Stef,

So far, I about all I have done with the xml parser is note that it can parse the data from my Garmin Forerunner.   Otherwise, I have used SIXX with good results, at least as far as I have pushed it.  Its code might give you some ideas that would apply to outside xml (maybe), or if you can control the schema, you might simply leave it to SIXX??

Bill



________________________________________
From: [hidden email] [[hidden email]] On Behalf Of stephane ducasse [[hidden email]]
Sent: Monday, February 21, 2011 4:11 AM
To: pharo Development
Subject: [Pharo-project] Pattern for reifying XML doc?

Hi guys

I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
Here is an example

<group>
    <groupName>Test2Cards</groupName>
    <cards><card>
    <cardName>All Is Dust</cardName>
    <manaCost>7</manaCost>
    <convertedManaCost>7</convertedManaCost>
    <type>Tribal Sorcery — Eldrazi</type>
    <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
    <rarity>mythic</rarity>
    <cardNumber>1</cardNumber>
    <artist>Jason Felix</artist>
    <kind>STD</kind>
    <expansionId>ELDRAZI</expansionId>
</card>
<card>
    <cardName>Kird Ape</cardName>
    <manaCost>G</manaCost>
    <convertedManaCost>1</convertedManaCost>
    <type>Tribal Sorcery — Eldrazi</type>
    <cardText>get +1/+1 when you control</cardText>
    <rarity>common</rarity>
    <cardNumber>33</cardNumber>
    <artist>Zork</artist>
    <kind>STD</kind>
    <expansionId>WORLDWAKE</expansionId>
</card></cards>
</group>

I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
and map that to objects. In the future I would like to have magritte like type of first class instance variables that
perform i/o in xml or other formats because this is always the same.

stef




Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Guillermo Polito
From the Java world, I know Digester http://commons.apache.org/digester/xmlrules.html, letting you map an xml to objects with a set of rules...
I don't know if that's what you were expecting as an answer :).

BTW, great example! :P

Guille

On Tue, Feb 22, 2011 at 11:39 AM, Schwab,Wilhelm K <[hidden email]> wrote:
Stef,

So far, I about all I have done with the xml parser is note that it can parse the data from my Garmin Forerunner.   Otherwise, I have used SIXX with good results, at least as far as I have pushed it.  Its code might give you some ideas that would apply to outside xml (maybe), or if you can control the schema, you might simply leave it to SIXX??

Bill



________________________________________
From: [hidden email] [[hidden email]] On Behalf Of stephane ducasse [[hidden email]]
Sent: Monday, February 21, 2011 4:11 AM
To: pharo Development
Subject: [Pharo-project] Pattern for reifying XML doc?

Hi guys

I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
Here is an example

<group>
   <groupName>Test2Cards</groupName>
   <cards><card>
   <cardName>All Is Dust</cardName>
   <manaCost>7</manaCost>
   <convertedManaCost>7</convertedManaCost>
   <type>Tribal Sorcery — Eldrazi</type>
   <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
   <rarity>mythic</rarity>
   <cardNumber>1</cardNumber>
   <artist>Jason Felix</artist>
   <kind>STD</kind>
   <expansionId>ELDRAZI</expansionId>
</card>
<card>
   <cardName>Kird Ape</cardName>
   <manaCost>G</manaCost>
   <convertedManaCost>1</convertedManaCost>
   <type>Tribal Sorcery — Eldrazi</type>
   <cardText>get +1/+1 when you control</cardText>
   <rarity>common</rarity>
   <cardNumber>33</cardNumber>
   <artist>Zork</artist>
   <kind>STD</kind>
   <expansionId>WORLDWAKE</expansionId>
</card></cards>
</group>

I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
and map that to objects. In the future I would like to have magritte like type of first class instance variables that
perform i/o in xml or other formats because this is always the same.

stef





Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Johan Brichau-2
In reply to this post by stephane ducasse
SIXX works very good to write/read objects, but it means you have to give up on your own xml schema.

And it's almost to easy to use:

        sws := SixxWriteStream newFileNamed: aFileName.
        sws nextPut: (self objectsToExport).
        sws close.

        srs := SixxReadStream readOnlyFileNamed: (aFileName).
        objects := OrderedCollection new.
        [srs atEnd] whileFalse: [objects add: srs next].
        srs close.

On 21 Feb 2011, at 10:11, stephane ducasse wrote:

> Hi guys
>
> I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
> Here is an example
>
> <group>
>    <groupName>Test2Cards</groupName>
>    <cards><card>
>    <cardName>All Is Dust</cardName>
>    <manaCost>7</manaCost>
>    <convertedManaCost>7</convertedManaCost>
>    <type>Tribal Sorcery — Eldrazi</type>
>    <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
>    <rarity>mythic</rarity>
>    <cardNumber>1</cardNumber>
>    <artist>Jason Felix</artist>
>    <kind>STD</kind>
>    <expansionId>ELDRAZI</expansionId>
> </card>
> <card>
>    <cardName>Kird Ape</cardName>
>    <manaCost>G</manaCost>
>    <convertedManaCost>1</convertedManaCost>
>    <type>Tribal Sorcery — Eldrazi</type>
>    <cardText>get +1/+1 when you control</cardText>
>    <rarity>common</rarity>
>    <cardNumber>33</cardNumber>
>    <artist>Zork</artist>
>    <kind>STD</kind>
>    <expansionId>WORLDWAKE</expansionId>
> </card></cards>
> </group>
>
> I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
> and map that to objects. In the future I would like to have magritte like type of first class instance variables that
> perform i/o in xml or other formats because this is always the same.
>
> stef
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

NorbertHartl
In reply to this post by stephane ducasse
Stef,

I'm using Magritte for all these cases. We developed a XML binding for magritte (http://source.lukas-renggli.ch/magritte2addons/) so it is easy as

User class>>descriptionEmailAddress
        ^ MAStringDescription new
                accessor: #emailAddress;
                priority: 100;
                label: 'Email Address';
                beRequired;
                beXmlAttribute;
                xmlAttributeName: 'email';
                yourself

but you need magritte and it doesn't sound you like to have a lot of dependencies. For read support I hacked something with Pastell for a quick and flexible read

Example class>>xmodel
        ^ Dictionary new;
                at: #name put: [:node|  (node / #Name ) contentString  ];
                at: #childObject put: [:node| ExampleReferencedClass node: (node / #ChildNode) first ];
                yourself

Example>>at: aSymbol
        ^ (self class xmodel at: aSymbol) value: self node

This is more flexible than easy. For your case you can map just the values. The biggest problem will probably be the type conversion. You could have setters like cardNumberString: that convert the type. But if I imagine you right now I imagine you faceplaming :)

Norbert

On 21.02.2011, at 10:11, stephane ducasse wrote:

> Hi guys
>
> I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
> Here is an example
>
> <group>
>    <groupName>Test2Cards</groupName>
>    <cards><card>
>    <cardName>All Is Dust</cardName>
>    <manaCost>7</manaCost>
>    <convertedManaCost>7</convertedManaCost>
>    <type>Tribal Sorcery — Eldrazi</type>
>    <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
>    <rarity>mythic</rarity>
>    <cardNumber>1</cardNumber>
>    <artist>Jason Felix</artist>
>    <kind>STD</kind>
>    <expansionId>ELDRAZI</expansionId>
> </card>
> <card>
>    <cardName>Kird Ape</cardName>
>    <manaCost>G</manaCost>
>    <convertedManaCost>1</convertedManaCost>
>    <type>Tribal Sorcery — Eldrazi</type>
>    <cardText>get +1/+1 when you control</cardText>
>    <rarity>common</rarity>
>    <cardNumber>33</cardNumber>
>    <artist>Zork</artist>
>    <kind>STD</kind>
>    <expansionId>WORLDWAKE</expansionId>
> </card></cards>
> </group>
>
> I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
> and map that to objects. In the future I would like to have magritte like type of first class instance variables that
> perform i/o in xml or other formats because this is always the same.
>
> stef
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Schwab,Wilhelm K
Mapping values reminds of a converting value adapter.  I will look shortly, but I fear I do not have a working one at present - I say that because only week ago, I created an aspect value adapter to get some ported code going.  It should not be too hard to throw together.


________________________________________
From: [hidden email] [[hidden email]] On Behalf Of Norbert Hartl [[hidden email]]
Sent: Tuesday, February 22, 2011 11:21 AM
To: [hidden email]
Subject: Re: [Pharo-project] Pattern for reifying XML doc?

Stef,

I'm using Magritte for all these cases. We developed a XML binding for magritte (http://source.lukas-renggli.ch/magritte2addons/) so it is easy as

User class>>descriptionEmailAddress
        ^ MAStringDescription new
                accessor: #emailAddress;
                priority: 100;
                label: 'Email Address';
                beRequired;
                beXmlAttribute;
                xmlAttributeName: 'email';
                yourself

but you need magritte and it doesn't sound you like to have a lot of dependencies. For read support I hacked something with Pastell for a quick and flexible read

Example class>>xmodel
        ^ Dictionary new;
                at: #name put: [:node|  (node / #Name ) contentString  ];
                at: #childObject put: [:node| ExampleReferencedClass node: (node / #ChildNode) first ];
                yourself

Example>>at: aSymbol
        ^ (self class xmodel at: aSymbol) value: self node

This is more flexible than easy. For your case you can map just the values. The biggest problem will probably be the type conversion. You could have setters like cardNumberString: that convert the type. But if I imagine you right now I imagine you faceplaming :)

Norbert

On 21.02.2011, at 10:11, stephane ducasse wrote:

> Hi guys
>
> I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
> Here is an example
>
> <group>
>    <groupName>Test2Cards</groupName>
>    <cards><card>
>    <cardName>All Is Dust</cardName>
>    <manaCost>7</manaCost>
>    <convertedManaCost>7</convertedManaCost>
>    <type>Tribal Sorcery — Eldrazi</type>
>    <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
>    <rarity>mythic</rarity>
>    <cardNumber>1</cardNumber>
>    <artist>Jason Felix</artist>
>    <kind>STD</kind>
>    <expansionId>ELDRAZI</expansionId>
> </card>
> <card>
>    <cardName>Kird Ape</cardName>
>    <manaCost>G</manaCost>
>    <convertedManaCost>1</convertedManaCost>
>    <type>Tribal Sorcery — Eldrazi</type>
>    <cardText>get +1/+1 when you control</cardText>
>    <rarity>common</rarity>
>    <cardNumber>33</cardNumber>
>    <artist>Zork</artist>
>    <kind>STD</kind>
>    <expansionId>WORLDWAKE</expansionId>
> </card></cards>
> </group>
>
> I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
> and map that to objects. In the future I would like to have magritte like type of first class instance variables that
> perform i/o in xml or other formats because this is always the same.
>
> stef
>
>
>



Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Stéphane Ducasse
In reply to this post by NorbertHartl
Yes I was thinking about using magritte too
because I did that too in the past. I will look at what you did.


I think that in the future we should really get
        - first class instance variables
        - internal to class or external mapper from to different formats like XML
On Feb 22, 2011, at 5:21 PM, Norbert Hartl wrote:

> Stef,
>
> I'm using Magritte for all these cases. We developed a XML binding for magritte (http://source.lukas-renggli.ch/magritte2addons/) so it is easy as
>
> User class>>descriptionEmailAddress
> ^ MAStringDescription new
> accessor: #emailAddress;
> priority: 100;
> label: 'Email Address';
> beRequired;
> beXmlAttribute;
> xmlAttributeName: 'email';
> yourself
>
> but you need magritte and it doesn't sound you like to have a lot of dependencies. For read support I hacked something with Pastell for a quick and flexible read
>
> Example class>>xmodel
> ^ Dictionary new;
> at: #name put: [:node|  (node / #Name ) contentString  ];
> at: #childObject put: [:node| ExampleReferencedClass node: (node / #ChildNode) first ];
> yourself
>
> Example>>at: aSymbol
> ^ (self class xmodel at: aSymbol) value: self node
>
> This is more flexible than easy. For your case you can map just the values. The biggest problem will probably be the type conversion. You could have setters like cardNumberString: that convert the type. But if I imagine you right now I imagine you faceplaming :)
>
> Norbert
>
> On 21.02.2011, at 10:11, stephane ducasse wrote:
>
>> Hi guys
>>
>> I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
>> Here is an example
>>
>> <group>
>>   <groupName>Test2Cards</groupName>
>>   <cards><card>
>>   <cardName>All Is Dust</cardName>
>>   <manaCost>7</manaCost>
>>   <convertedManaCost>7</convertedManaCost>
>>   <type>Tribal Sorcery — Eldrazi</type>
>>   <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
>>   <rarity>mythic</rarity>
>>   <cardNumber>1</cardNumber>
>>   <artist>Jason Felix</artist>
>>   <kind>STD</kind>
>>   <expansionId>ELDRAZI</expansionId>
>> </card>
>> <card>
>>   <cardName>Kird Ape</cardName>
>>   <manaCost>G</manaCost>
>>   <convertedManaCost>1</convertedManaCost>
>>   <type>Tribal Sorcery — Eldrazi</type>
>>   <cardText>get +1/+1 when you control</cardText>
>>   <rarity>common</rarity>
>>   <cardNumber>33</cardNumber>
>>   <artist>Zork</artist>
>>   <kind>STD</kind>
>>   <expansionId>WORLDWAKE</expansionId>
>> </card></cards>
>> </group>
>>
>> I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
>> and map that to objects. In the future I would like to have magritte like type of first class instance variables that
>> perform i/o in xml or other formats because this is always the same.
>>
>> stef
>>
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Stéphane Ducasse
In reply to this post by Johan Brichau-2
Thanks johan
What I like with having just xml with my format is that I can hack the files without breaking the objects it represents.
I can evolve my objects without losing my filed backup in xml.

On Feb 22, 2011, at 5:07 PM, Johan Brichau wrote:

> SIXX works very good to write/read objects, but it means you have to give up on your own xml schema.
>
> And it's almost to easy to use:
>
> sws := SixxWriteStream newFileNamed: aFileName.
> sws nextPut: (self objectsToExport).
> sws close.
>
> srs := SixxReadStream readOnlyFileNamed: (aFileName).
> objects := OrderedCollection new.
> [srs atEnd] whileFalse: [objects add: srs next].
> srs close.
>
> On 21 Feb 2011, at 10:11, stephane ducasse wrote:
>
>> Hi guys
>>
>> I often encounter the following situation: I have an xmldoc and I want to load it and get the corresponding objects.
>> Here is an example
>>
>> <group>
>>   <groupName>Test2Cards</groupName>
>>   <cards><card>
>>   <cardName>All Is Dust</cardName>
>>   <manaCost>7</manaCost>
>>   <convertedManaCost>7</convertedManaCost>
>>   <type>Tribal Sorcery — Eldrazi</type>
>>   <cardText>Each player sacrifices all colored permanents he or she controls.</cardText>
>>   <rarity>mythic</rarity>
>>   <cardNumber>1</cardNumber>
>>   <artist>Jason Felix</artist>
>>   <kind>STD</kind>
>>   <expansionId>ELDRAZI</expansionId>
>> </card>
>> <card>
>>   <cardName>Kird Ape</cardName>
>>   <manaCost>G</manaCost>
>>   <convertedManaCost>1</convertedManaCost>
>>   <type>Tribal Sorcery — Eldrazi</type>
>>   <cardText>get +1/+1 when you control</cardText>
>>   <rarity>common</rarity>
>>   <cardNumber>33</cardNumber>
>>   <artist>Zork</artist>
>>   <kind>STD</kind>
>>   <expansionId>WORLDWAKE</expansionId>
>> </card></cards>
>> </group>
>>
>> I have card and group. But I could have several kind of nodes. So I was wondering if there are patterns to read in
>> and map that to objects. In the future I would like to have magritte like type of first class instance variables that
>> perform i/o in xml or other formats because this is always the same.
>>
>> stef
>>
>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Pattern for reifying XML doc?

Stéphane Ducasse
In reply to this post by Guillermo Polito

On Feb 22, 2011, at 3:41 PM, Guillermo Polito wrote:

> From the Java world, I know Digester http://commons.apache.org/digester/xmlrules.html, letting you map an xml to objects with a set of rules...
> I don't know if that's what you were expecting as an answer :).
>
> BTW, great example! :P

Magic is cool :)
Henrik has crazy games too :)

Stef