On 20.07.2010, at 19:15, Larry Gadallah wrote:
> On 19 July 2010 12:16, <[email protected]> wrote:
>> Message: 6
>> Date: Mon, 19 Jul 2010 21:15:54 +0200
>> From: Norbert Hartl <[email protected]>
>> Subject: Re: [Pharo-project] XML Demarshalling
>> To: [email protected]
>> Message-ID: <[email protected]>
>> Content-Type: text/plain; charset=us-ascii
>>
>>
>> On 16.07.2010, at 18:46, Larry Gadallah wrote:
>>
>>> Hi all:
>>>
>>> I am a noob searching for an example of how to demarshall a set of
>>> simple and complex XML nodes into an object using Pastell or something
>>> similar. I have found many examples of how to pick out all of the
>>> elements of a certain type into a collection, but I'm trying to grab
>>> the data from a mixed collection of (~20) elements that are children
>>> of a particular element type and unpack them into an object.
>>>
>>> Does anyone know of some good examples of how to do this using Pastell
>>> or something similar?
>>>
>> Can you give more detailled information about how the XML maps onto the data
>> model? I mean if you have childr elements will that map to objects that are
>> referenced similar? Do attributes map to variables and such? Or do you want
>> to freely define how the structure of the XML resembles into your objects?
>>
>> Norbert
>
> Hi Norbert:
>
> Alas, I am laboring under the burden of being a newbie to both Pharo
> and XML. It seems to me that it would be better to be able to
> dynamically pick up all of the child elements of an element of
> interest, however, I can't imagine how to define an object that
> doesn't know it's own contents until run-time. In addition, in my
> case, some of the child elements are complex (i.e. they also contain
> children) which complicates the traversal a little bit. I'm assuming
> for now that I will have to read the source XML, define the instance
> variables of my object based on them, and simply populate them at run
> time by traversing the XML tree. If the XML model changes, the object
> definition will have to change too.
>
Hi again,
just yesterday I discovered that I have a similar problem to solve. Usually I
would try to solve it with magritte but this time I need some quick and dirty
hack to achieve the goal. And somehow I like Pastell.
Let's take an XML Snippet
<Root>
<Name>Mister RootNode</Name>
<SubNode>
<Name>Mister SubNode</Name>
</SubNode>
</Root>
And now let's assume you want everything under Root in an object of class
RootClass. In the case of SubNode you want a new object from class SubNodeClass
that is referenced from RootClass object. In this example both classes
RootClass and SubNodeClass are subclasses of NodeBaseClass
NodeBaseClass class>>node: aNode
^ self new
node: aNode
NodeBaseClass>>node
^ node
NodeBaseClass>>node: aNode
node := aNode
NodeBaseClass>>at: aSymbol
^ (self class xmodel at: aSymbol) value: self node
Here is the RootClass:
RootClass class>>xmodel
^ Dictionary new;
at: #name put: [:node| (node / #Name ) contentString ];
at: #subObject put: [:node| SubNodeClass node: (node /
#SubNode) first ];
yourself
and another class that is referenced from RootClass. Btw. "(node / #Name)" is
using Pastell
SubNodeClass class>>xmodel
^ Dictionary new;
at: #name put: [:node| (node / #Name ) contentString ];
yourself
Then you could do:
document := XMLDOMParser parse....
rootObject := RootClass node: document root.
Transcript show: ((rootObject at: #subObject) at: #name
and you should get "Mister SubNode" . I hope there aren't too much errors in
there. The basic idea is that every object gets an XML node and a description
how to retrieve the values relative to this node. In the case of #name just a
string is received. In the case of #subObject you can see how this gets
recursive. #subObject returns itself an object that is just initialized with an
XML node. Just like the rootObject
This is just a quick way to get some objects from an XML file. There is much
room for improvement. I just wanted to give you an idea how you could solve it.
Hope this helps,
Norbert
_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project