Re: Help with XML-SAX program ... it's driving me nuts ...



mitsura@xxxxxxxxx wrote:

> I need to read a simle XML file. For this I use the SAX parser. So far
> so good. The XML file consist out of number of "Service" object with
> each object a set of attributes.

> The strange thing is that for some reason, the attributes for all the
> objects are being updated. I don't understand why this happens.

you're using the same dictionary for all Service elements:

obj.attributes = self.attribs

adds a reference to the attribs dictionary; it doesn't make a copy (if it
did, your code wouldn't work anyway).

changing

self.attribs.clear()

to

self.attribs = {} # use a new dict for the next round

fixes this.

> It's driving me nuts. I have spend hours going through this very simple
> code, but I can't find what's wrong.

simple? fwiw, here's the corresponding ElementTree solution:

import elementtree.ElementTree as ET

for event, elem in ET.iterparse("kd.xml"):
if elem.tag == "Service":
d = {}
for e in elem.findall("Attribute"):
d[e.findtext("Name")] = e.findtext("Value")
print elem.findtext("Name"), d

(tweak as necessary)

</F>



.



Relevant Pages

  • XML SAX parser bug?
    ... I think I ran into a bug in the XML SAX parser. ... Let me explain: the XML file contains a few thousand lines like this: ... the SAX parser misreads the line. ... I put a 'print characters' line in the 'characters' method of the ...
    (comp.lang.python)
  • Re: Parsing XML from RPGLE
    ... The disadvantage is that I do not yet support a SAX parser. ... On the other hand (if you have the Toolkit) you can install the relevant option and look at the samples on your AS/400. ... could you please post your example on how to parse an XML file? ... through a DOM object and creating a DOM object from scratch. ...
    (comp.sys.ibm.as400.misc)
  • MSXML SAX parsing with C code
    ... I am trying to parse an xml file using MSXML SAX parser in C. ... Please guide me on how to proceed with parsing. ...
    (microsoft.public.vc.language)
  • xerces/SAX xml search
    ... find locations (line/column) of certain elements and attributes within ... will search through an xml file as it parses. ... SAX parser and an empty handler. ... the parse search. ...
    (comp.text.xml)
  • Re: Using PHP to generate HTML from the contents of an XML file?
    ... >> XML file, and use the contents of this file to generate a HTML page? ... >> I need to implement this using PHP and have very little experience. ... My experience is that XSLT is the most straightforward way to produce ... SAX parser - at least as soon as the transformation is just a bit more ...
    (comp.lang.php)