[Newbies] xml file query and object mapping

Edward Stow ed.stow at gmail.com
Fri Jun 30 01:47:22 UTC 2006


Cedrick

I would use the SAX parser.  If you only need to extract one 'record'
from the document then its not too hard.

Start by subclassing SAXHandler and define some methods to handle the events.

Suggest that you initially define two methods to get started. (Code is
not tested)

startElement: elementName attributeList: attributeList
    "currentElement is an instance variable"
     currentElement := elementName.

characters: aString
     (currentElement = 'ZONE') & (aString = '6504') ifTrue: [ .... ]

For more inspiration look to see how XMLDOMParser works - itself a
subclass of SAXHandler.   The hardest part of writng a SAXParser is
that you need to keep track of the state of the parser so that you
handle the events and construct some objects that are meaningful for
your problem.


On 30/06/06, cdrick <cdrick65 at gmail.com> wrote:
> Hi all
>
> I've got xml files that I receive from Meteo France (weather forecast) that
> I have to query and map to some objects
> I've tried several stuff like parsing the file, selecting only some
> elements,  displaying it with a WATree but I'm a bit stuck as none results I
> got are good enough.
>
> The structure is like behind:
>
> <?xml version="1.0" encoding="UTF-8" ?>...
> <donnees>
> <dates>...</dates>
> <prevision>
>  <prev num="1">    // a prev concern a zone for 3 hours - there are 18 prev
> for a given zone
>   <ZONE>6403</ZONE>
>   <VALIDITE>200606292000</VALIDITE>
>    <JJ>20060629</JJ>
>   <HH>2000</HH>
>   <T>24</T>
>   <PICTO>P6</PICTO>
>   <DD>315</DD>
>   <FF>10</FF>
>   <RAF></RAF>
>   <ISO_ZERO>4100</ISO_ZERO>
>   <LIMIT_PLUIE_NEIGE></LIMIT_PLUIE_NEIGE>
>   <W1>0</W1>
>   <W2></W2>
>   <NEBUL_TOT>3</NEBUL_TOT>
>   <BASE_NUAGE></BASE_NUAGE>
>   <SOMMET_NUAGE></SOMMET_NUAGE>
>   <FF1500M>5</FF1500M>
>   <FF3000M>25</FF3000M>
>   <RR6>0.4</RR6>
>   <W3></W3>
>   <EPAIS_NEIGE></EPAIS_NEIGE>
>   <VIGILANCE></VIGILANCE></prev>
>
>  ....   around 450 prev (18 for each ZONE)
>
>   <prevQ num="1">   // this are prevision for the whole day - 4 prevQ in
> each file for a given zone
>   <ZONE>3107</ZONE>
>   <VALIDITE>200606290000</VALIDITE>
>   <JJ>20060629</JJ>
>   <TN>15</TN>
>   <TX>26</TX>
>   <RR24>1.0</RR24>
>   <UN>60</UN>
>   <UX>100</UX></prevQ>
> ...
> </prevision>
> </donnees>
>
>
> What I need to do is quering the file according to the some attribute (ZONE,
> VALIDITE, JJ and HH) so as to get the different value that characterize the
> weather forecast. The only thing I can do is selecting nodes accoring to one
> parameter, for instance all prevQ element of zone 6504 (using Xpath).
>
> | xml document path results |
>     xml := (StandardFileStream readOnlyFileNamed:
> 'ressources/MF/backup/sympo0805200617h00.xml') contents.
>     document := XMLDOMParser parseDocumentFrom: xml readStream.
>     path := XPath for: 'prevision/prev'.
>     results := path in: document.
>     ^ results
>         select: [:xmlElem | (xmlElem elementAt: #ZONE) contents first
> string = '6504']
>
>
> I'm a bit lost now. Is it possible to map to objects ?  do I need to work
> directly on xml file by xsl transformation ? I don't really understand how
> to use a sax parser (I always do a full dom parse that I store in an inst
> var).
>
> Do you have advices, pointers or other good practises that can help me ?
>
> Thanks
>
> Cédrick
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
>
>


-- 

Edward Stow


More information about the Beginners mailing list