1

I am new to using expat. I am trying to figure out how XML_SetUserData is intended to do. In my case, I have a list of class A objects and I need to populate this list as I parse the XML stream.

So far, I was planning on using a singleton to represent this single list. Is the XML_SetUserData to eliminate the need for my list being a singleton?

skaffman
  • 398,947
  • 96
  • 818
  • 769
reza
  • 5,972
  • 15
  • 84
  • 126

1 Answers1

2

Yes, it does. Just pass XML_SetUserData a pointer to whatever struct or class holds the necessary state, and cast it back from (void*) inside your handler.

A useful trick is to define a base class with static callback member functions that forward calls from expat to virtual member functions. In this scenario, XML_SetUserData provides a convenient mechanism to pass the this pointer through to the static callbacks.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
  • any advantage other than eliminating a global? I still need to keep track of what "element" is being parsed in my data structure, global or not, both. Correct? – reza Aug 30 '11 at 01:25
  • @reza: Yes, you have to do all your own tracking. I've added a common usage scenario that highlights one additional benefit (though avoiding globals is the primary motivator for passing user data through an API in this manner). – Marcelo Cantos Aug 30 '11 at 01:32