Re: [xml] search for a match in the tree and return a node pointer


I want to search for a match in the tree and return a node pointer. I don't know what functions to use in libxml2. Here's my code:

??? findContent (const char* searchStr, xmlNodePtr cur)
xmlNodePtr cur_node = NULL;
for (cur_node = cur; cur_node; cur_node = cur_node->next)
if (!xmlStrcmp(cur->name, (const xmlChar*)searchChar)) return ???;
else if (!xmlStrcmp(cur->name, (const xmlChar*)searchChar)) return ???;
        I didn't undestand. You're using searchStr and checking node's
name against searchChar??! Well, considering it a typo this snippet
seems to work, but be aware both conditionals are equal.
        You can return whatever you like: a maching node pointer, a
pointer to content node which is child of maching node, the content
itself (xmlNodeListGetString).
        For the sake of consistency, I think findContent() should
be declared using searchStr as const xmlChar * and casts searchStr
when calling findContent().

Then, in the next function, FindAllContents(???) //something like, xmlNodePtr ptr
//location pointer from FindContent() is the input, output returns all items and contents for that node; what structures or functions should I use?

A code example would be helpful.  Thank you.

        Since looks like you don't want to use XPath (which should be
*lot* easier) you must walk throught the entire tree, mostly using a lot
of loops :) A recursive function should do the trick.


Lucas Brasilino
brasilino recife pe gov br
Emprel -        Empresa Municipal de Informatica (pt_BR)
                Municipal Computing Enterprise (en_US)
Recife - Pernambuco - Brasil
Fone: +55-81-34167078

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]