On Fri, 16 Jul 2010, Larry Gadallah wrote:
On 16 July 2010 11:54, <[email protected]> wrote:
Message: 7
Date: Fri, 16 Jul 2010 15:52:27 -0300
From: Hern?n Morales Durand <[email protected]>
Subject: Re: [Pharo-project] XML Demarshalling
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
How big is your XML file? Molecular biology?
Hern?n
2010/7/16 Larry Gadallah <[email protected]>:
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?
Hernan:
The files are definitely not molecular biology. They are quite small,
and they represent simple catalog items with typical attributes (i.e.
item number, name, rating, etc.).
The element hierarchy looks something like this:
<catalog>
<status>
</status>
<products>
<list>
<product>
<id></id>
<name></name>
<reviews>
...
</reviews>
<labels>
....
<labels>
<type></type>
</product>
</list>
</products>
</catalog>
So, what I want to do is be able to demarshall <product> elements into
a product object. I have seen plenty of examples of how to grab the
content of all of the name elements, but none showing how to pull all
of the sub-elements of a given element type into an object.
If your xml documents are small (fit into memory), then the best is to use
XMLDOMParser to build you a dom tree. Then you can do all kind of queries
depending on the structure. The following example assumes a flat structure
and non-repeating child nodes:
doc := '<yourxmldocument/>'
dom := XMLDOMParser parseDocumentFrom: doc readStream.
objects := Array streamContents: [ :stream |
dom tagsNamed: #product do: [ :tag |
| object |
object := Dictionary new.
tag elementsDo: [ :element |
object at: element name put: element contentString ].
stream nextPut: object ] ].
(Btw the idea that XMLElement >> #tag is returning a symbol and the
#tags*[dD]o: methods are expecting a symbol for tag is a very bad idea. It
degrades performance.)
And where did the XML-Parser go from Pharo?
Levente
Thanks,
--
Larry Gadallah, VE6VQ/W7 lgadallah AT gmail DOT com
PGP Sig: 917E DDB7 C911 9EC1 0CD9 C06B 06C4 835F 0BB8 7336
_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________
Pharo-project mailing list
[email protected]
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project