View Full Version : python-xml headache

11 November 2010, 04: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

if curveEl !=0:
return curveEl
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\\xml\dom\ in module removeChild
# raise xml.dom.NotFoundErr() #

Can anybody help?


11 November 2010, 08:36 AM
what xml module are you using?
what is findCurveElement? what does it return? does it work if you use the return manually?

11 November 2010, 08: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.

11 November 2010, 09: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:

11 November 2010, 05: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

11 November 2010, 10:57 PM
can you attach an example xml where it does not work as well?

11 November 2010, 11:17 PM
<curveShape name="circle">
<code codeString="some code" orientation="ZY"/>

11 November 2010, 09: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"?>
<curveShape name="circle1" codeString="code goes here" orientation="ZY"/>
<curveShape name="circle2" codeString="code goes here" orientation="ZY"/>

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 November 2010, 09: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.