[Pharo-project] Pattern for reifying XML doc?

Norbert Hartl norbert at hartl.name
Tue Feb 22 17:21:04 CET 2011


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
> 
> 
> 





More information about the Pharo-project mailing list