[gdal-dev] RE: CPLXMLNode, no GetChilds()'like method ?

Aurélien Kamel aurelien.kamel at magellium.fr
Fri Oct 17 03:49:35 EDT 2008


Frank,

Actually I modified my code, now I drop all the nodes that starts with <? or
<! at the beginning of my file so that I only have the "main" node (<svg in
my case) before parsing the buffer with CPLParseXMLString. Maybe not the
best way to do the job but it works.

Thanks for your help,
Cheers,
aurel

-----Message d'origine-----
De : Frank Warmerdam [mailto:warmerdam at pobox.com] 
Envoyé : jeudi 16 octobre 2008 17:27
À : Aurélien Kamel
Cc : gdal-dev at lists.osgeo.org
Objet : Re: [gdal-dev] RE: CPLXMLNode, no GetChilds()'like method ?

Aurélien Kamel wrote:
> Actually I have one more question for you. Imagine you have an xml file
like
> this :
> 
> ---- start of xml file ----
> <?xml version="1.0"?>
> 
> <a b="xxx">
>   <b>
>     ...
>   </b>
> </a>
> ---- end of xml file ----
> 
> How can you access to the node "a" if you don't know its name ? I tried to
> do a recursive function that would loop on all the nodes of the file
> starting from ROOT.
> 
> With the following code I'll only recurse on the "<?xml version="1.0"?>"
> node.
> 
> bool
> MyDriver::parse(const char *filename) {
>   CPLXMLNode* xmlNode = CPLParseXMLFile(filename);
>   If (!xmlNode) return false;
>   return parseLoop(xmlNode);
> }
> 
> bool
> MyDriver::parseLoop(CPLXMLNode *node) {
>   std::cout << "Currently recursing on node: " << node->pszValue <<
> std::endl;
>   
>   for (CPLXMLNode *brother = node->psChild;
>        brother;
>        brother = brother->psNext)
>     if (!parseLoop(brother)) return false;
> 
>   return true;
> }
> 
> Any idea how I'll could recurse on all the nodes that are child of ROOT ?

Aurel,

The code looks ok except that it misses the siblings of the root node.
In a case like the above file the "a" element will be a sibling of the
<?xml...> node, not a child.

There are numerous examples of code for recursing and searching the XML
document in gdal/port/cpl_minixml.cpp.

Best regards,
-- 
---------------------------------------+------------------------------------
--
I set the clouds in motion - turn up   | Frank Warmerdam,
warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the gdal-dev mailing list