473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Hi,

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.

I read through the XML file and for each "<Service>" entry I create a
new service object.
When I am in the "<Service>" part of the XML file and I encounter
"<Attributes>" then I store these attributes into a Python dictionary.
At the end of the "</Service>" tag I create the actual object and pass
the attributes directory to it.
The strange thing is that for some reason, the attributes for all the
objects are being updated. I don't understand why this happens.

I included the code and a example of the XML file (kd.xml)
The output should be:
Obj: name2 attribs: {}
Obj: name attribs: {attrib1, val1, atttrib1.1, val1.1}
Obj: name1 attribs: {attrib2, val 2}

but it is:
Obj: name2 attribs: {}
Obj: name attribs: {}
Obj: name1 attribs: {}

It's driving me nuts. I have spend hours going through this very simple
code, but I can't find what's wrong.
Any help is really very much appreciated.

With kind regards,

Kris
XML File (kd.xml):
"
<?xml version='1.0' ?>
<Services>
<Service>
<Name>name</Name>
<Label>label</Label>
<Icon>/opt/OV/www/htdocs/ito_op/images/service.32.gif</Icon>
<Status>
<Normal/>
</Status>
<Depth>1</Depth>
<Attribute>
<Name>Attrib1</Name>
<Value>Val1</Value>
</Attribute>
<Attribute>
<Name>Attrib1.1</Name>
<Value>Val1.1</Value>
</Attribute>
</Service>
<Service>
<Name>name1</Name>
<Label>label1</Label>
<Icon>/opt/OV/www/htdocs/ito_op/images/service.32.gif</Icon>
<Status>
<Normal/>
</Status>
<Depth>1</Depth>
<Attribute>
<Name>Attrib2</Name>
<Value>val 2</Value>
</Attribute>
</Service>
<Service>
<Name>name2</Name>
<Label>label2</Label>
<Icon>/opt/OV/www/htdocs/ito_op/images/service.32.gif</Icon>
<Status>
<Normal/>
</Status>
<Depth>1</Depth>
</Service>
</Services>
"
Program:
"
import sys
import string
import images
import os
import cStringIO
import xml.sax
from wxPython.wx import *
from Main import opj
from xml.sax.handler import *
from xml.sax import make_parser

class ServiceObj:

def __init__(self,name):

# Serivce object properties
self.name = name
self.attributes = {}

class OpenSNXML(ContentHandler):

# Service Object Tags
inService = 0
inServiceAttribute = 0
inServiceName = 0
inAttributeName = 0
inAttributeValue = 0
objName = ""
objLabel = ""

attribs = {}
attribName = ""
attribValue = ""

def startElement(self, name, attrs):

if name == "Service":
self.inService = 1

if self.inService == 1:
if name == "Attribute":
print "Attribute start"
self.inServiceAttribute = 1

if name == "Name" and ( self.inServiceAttribute == 0 ):
self.inServiceName = 1

if name == "Name" and (self.inServiceAttribute == 1):
self.inAttributeName = 1

if name == "Value" and (self.inServiceAttribute == 1):
self.inAttributeValue = 1

def characters(self, characters):
if self.inServiceName == 1:
self.objName = self.objName + characters

if self.inAttributeName == 1:
#print "Attribute Name: ", characters
self.attribName = self.attribName + characters

if self.inAttributeValue == 1:
#print "Attribute Value: ", characters
self.attribValue = self.attribValue + characters

def endElement(self, name):
mD = 1

if name == "Service":

self.inService = 0

# If the object already exists, update the existing object
if AllServiceObjectsFromXMLFile.has_key(self.objName) :
#print "Object: ", self.objName, " already defined in exists in
dir"
obj = AllServiceObjectsFromXMLFile[self.objName]
else:
obj = ServiceObj(self.objName)

obj.attributes = self.attribs

AllServiceObjectsFromXMLFile[self.objName] = obj

del obj

self.objName = ""
self.attribs.clear()

if name == "Attribute":
self.inServiceAttribute = 0
print "Attrib name: ", self.attribName
print "Attrib value: ", self.attribValue

self.attribs[self.attribName] = self.attribValue
self.attribName = ""
self.attribValue = ""

print "Attrib dir: ", self.attribs
print "Attribute stop"

if name == "Name":
self.inServiceName = 0
self.inAttributeName = 0

if name == "Value":
self.inAttributeValue = 0
# Main
AllServiceObjectsFromXMLFile = {}

GetAllObjectsFromXMLfilePS = xml.sax.make_parser()
GetAllObjectsFromXMLfileHD = OpenSNXML()
GetAllObjectsFromXMLfilePS.setContentHandler(GetAl lObjectsFromXMLfileHD)
GetAllObjectsFromXMLfilePS.parse("kd.xml")

keys = AllServiceObjectsFromXMLFile.keys()

for key in keys:
obj = AllServiceObjectsFromXMLFile[key]
print "Obj: ", obj.name, "attribs: ", obj.attributes
"

Jan 31 '06 #1
2 1492
mi*****@skynet.be 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>

Jan 31 '06 #2
Thanks for the feedback!
I will certainly look at the elementtree stuff (I am new to Python so I
still need to find my way around)

Jan 31 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: marktm | last post by:
Hi- I am using the following code and want to retrieve an attributes value. I want to loop through the xml file and add the attribute values to an array. The line in question is marked with...
2
by: marktm | last post by:
Hi- I am using the following code and want to retrieve an attributes value. I want to loop through the xml file and add the attribute values to an array. The line in question is marked with...
4
by: David | last post by:
Hello , I'm trying to parse an XML document a get spicific tags such as email in the code below. I'm using xerces 2.4. However I don't manage to get the value for the email. Can anybody help. ...
0
by: Marko Maehner | last post by:
Hi, I have a strange problem with my xml file. In the schema of this xml file I have set one column to autoincrement. When I enter the data in my xml file directly, the autoincrement-column gets...
1
by: Leepe | last post by:
example: I have a skill object that implements the XmlSerializable interface Implementing the getschema gives me some unwanted generations towards the wsdl.exe Reference.cs generation. skill is...
0
by: BillB | last post by:
Hello, I have been looking for a C# example to call a SQL Stored Procedure and parse an XML doc (foo.xml). The Stored Procedure should have 3 ins and one out : IN Example: 1- blobtext ntext...
2
by: -D- | last post by:
I'm taking my first stab at using xml, so please bear with my novice questions and understanding of xml. I'm trying to create an xml file that holds all my website navigation. If I understand...
1
by: Domini | last post by:
Hi all, I need urgent help with an xml issue. Let me explain the scenario: My VB.NET app needs to read an xml (e.g. Sales Orders). This xml contains say 10 sales orders. Next the app needs to split...
1
by: redgrL86 | last post by:
Hi, I am trying to create an XSL file that can be applied to numerous XML files (all XML files have similar structure but varying length). The XSL and abbreviated XML codes are below. Within the XSL...
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.