PDA

View Full Version : python-xml headache


sauzer
11-23-2010, 03:12 AM
So I can't figure this out! I have been looking at this code for hours and I cannot find anything wrong with it. I went on several sites, did research and looked at my python xml book, but I think I am doing everything right.... however I know there is something wrong otherwise there would be no error :)

this is the code I am writing:

def removeCurve(curveName):
"""Removes a curve node from the XML file"""
if os.path.getsize(xmlFile) > 0 :
doc = parse(xmlFile)
if curveElementExists(curveName) != 0:
root = doc.documentElement
print root.nodeName
curveEl=findCurveElement(curveName)

if curveEl !=0:
root.removeChild(curveEl)
writeCurveFile(doc)
return curveEl
else:
raise NameError('There is no curve OBJECT named "%s" in the XML file' % curveName)
raise NameError('There is no curve named "%s"' % curveName)

This is the error I get;
# line 164 of file C:\Program Files\Autodesk\Maya2010\bin\python26.zip\xml\dom\minidom.py in module removeChild
# raise xml.dom.NotFoundErr() #

Can anybody help?

Thanks!

ewerybody
11-23-2010, 07:36 AM
what xml module are you using?
what is findCurveElement? what does it return? does it work if you use the return manually?

sauzer
11-23-2010, 07:42 AM
This is what I am importing:
import xml.dom.minidom as xmlDom
from xml.dom.minidom import parse

The findCurveElement(curveName) function does work by itself. I have tested several times, so I am pretty sure.

ewerybody
11-23-2010, 08:45 AM
and what does findCurveElement return? If it does not find a thing what comes out of it? Is it really 0 like you check for? Or is it maybe "" empty string or is it even a None?
I had several of that cases:if variable is not None:

sauzer
11-23-2010, 04:38 PM
This is the code for findCurveElement:

def findCurveElement(curveName):
"""If the passed curve exists in the XML document, then it returns the
curve node OBJECT. If it cannot find it then returns the value '0'"""
doc = parse(xmlFile)
root = doc.documentElement
curvesList = root.getElementsByTagName('curveShape')
for curve in curvesList:
if curve.getAttribute('name') == curveName:
return curve
return 0

and this is what I get if I print the return of findCurveElement:
<DOM Element: curveShape at 0x1b475788>

which is what I would expect

ewerybody
11-23-2010, 09:57 PM
can you attach an example xml where it does not work as well?

sauzer
11-23-2010, 10:17 PM
<curveShapesList>
<curveShape name="circle">
<code codeString="some code" orientation="ZY"/>
</curveShape>

Pyrokinesis
11-27-2010, 08:39 AM
Hey sauzer,
Sorry no offense, but I'm a little confused by reading your post, I cant figure out what you are trying to do... So I'll just give some blanket advice and hope its useful.

Your xml is formatted poorly and causing errors, heres a cleaner example:

<?xml version="1.0" encoding="UTF-8"?>
<curveShapesList>
<curveShape name="circle1" codeString="code goes here" orientation="ZY"/>
<curveShape name="circle2" codeString="code goes here" orientation="ZY"/>
</curveShapesList>

Notice the header, I think your example was also missing a closing curveShapesList node.
ElementTree or cElementTree is much faster and easier to read than dom.

simple example:

from xml.etree import ElementTree as elementTree

xmlPath = '/path/to/your/xmlExample.xml'
xmlRoot = elementTree.parse(xmlPath).getroot()
curveShapes = xmlRoot.findall('curveShape')

if curveShapes:
for curveShape in curveShapes:
name = curveShape.get('name')
orientation = curveShape.get('orientation')
codeString = curveShape.get('codeString')

Deleting is as simple as:
del curveShape

Again I'm not sure exactly what your end goal is, I hope this helps.

Good Luck.

CGTalk Moderation
11-27-2010, 08:39 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.