Hi Evan,

On Fri, Jul 15, 2011 at 7:39 PM, Evan Klinger <eklin...@gmail.com> wrote:
> Hello,
> I would like to use the xmlTextReader interface for parsing my XML
> file, but in the parsing routine handler that is called, I need to set
> some of my own data depending upon the element's value, but I don't
> see any way to pass in my own user data or a void * like you can do in
> expat. Is this possible? I don't want to make my struct a global - I'd
> like it to be passed into the parsing function directly.

You don't have to bother with things like that when using xmlTextReader.

expat operates on the "don't call us, we'll call you" principle. When
you process an XML document with multiple XML elements, your
start_element handler gets called once for each XML element. Local
context is lost between these callbacks. This is why most callback
interfaces allow passing some sort of user context, to keep persistent
data.

In contrast, with xmlTextReader, *your* code calls libxml2. This means
that any local data is readily available after the call to libxml2
returns control to your code. Processing an XML document with multiple
elements can be as simple as a for loop:

int num_elements = 0;
for (success = xmlTextReaderRead(...); success == 1; success =
xmlTextReaderRead(...))
{
  if (xmlTextReaderNodeType(...) == XML_READER_TYPE_ELEMENT) {
    // do something
    ++num_elements;
  }
}

In this case, all local variables remain in scope for each XML element
processed, like num_elements in the above example.

Hope this helps,
Csaba
-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml@gnome.org
http://mail.gnome.org/mailman/listinfo/xml

Reply via email to