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>
.... around 450 prev (18 for each ZONE)<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> <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> </prevision><TN>15</TN> <TX>26</TX> <RR24>1.0</RR24> <UN>60</UN> <UX>100</UX></prevQ> ... </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 [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
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 <[hidden email]> 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 > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners > > > -- Edward Stow _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
thanks Edward ;). I'm having a look right now...
I've not well understood what you mean by: "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." but I'll probably see after digging a bit more in saxhandler _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by cedreek
Cédrick,
The basic difference between SAX and DOM is that SAX is like a stream that as it is read provides information to you to build the objects you need, while DOM is more like a tree or object model that can be traversed like regular objects. They both have their advantages. The big advantage for SAX is in parsing large files. Since it acts like a stream you can build objects on the fly and not have to parse the entire file before getting a single object. DOM is much easier to use since it builds a tree for you that can be traversed. For DOM to create an object you need to build your own object form the data that each node of the tree provides to you. The tree is a collection of collections with each item an XMLElement that can respond with queries like elementAt: #... you can iterate over the collection creating objects as you go. For Sax you wait for a tag that indicates starting a new object then you create the object and for each element that comes after you add data to your object until you hit the end object tag. If you are going to XML then you should learn both (which it seems you are doing). Good luck with it and feel free to let us know how you are doing with it. If you need more help let us know. Ron Teitelbaum President / Principal Software Engineer US Medical Record Specialists [hidden email] _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |