[Expat-discuss] Question/coding about expat and attributes

Fred L. Drake, Jr. fdrake@acm.org
Wed Aug 7 11:50:04 2002


Mitchell Szczepanczyk writes:
 > The program I've been writing parses and XML document and puts the 
 > XML document into a hierarchical node structure.  Each start tag gets its
 > own node, nodes have pointers for names and attributes and nodes are
 > linked with one another.  Everything seems to work fine except for the
 > attributes: whenever I try to put a single set of attributes (via the
 > starthandler element) into a node, my program seems to put the most recent
 > set of attributes into all of the nodes in the structure.  And yet,
 > there's nothing in the code (at least that I can see) which would suggest
 > otherwise.

It's not hard to see:  you're storing a reference to a buffer of
attributes into your structure, but you don't own the memory being
referenced.  Your node structure needs to store a copy of the memory,
so there's a bit of work you need to do to allocate memory and make a
copy of the attribute information.

Expat uses the same result buffers as much as it can to avoid making
additional allocations (important for performance), allowing you to
copy only as much data as you need to for your application.


  -Fred

-- 
Fred L. Drake, Jr.  <fdrake at acm.org>
PythonLabs at Zope Corporation