python-xml headache

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

Thread Tools Display Modes
  11 November 2010
python-xml headache

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\m in module removeChild
# raise xml.dom.NotFoundErr() #

Can anybody help?

  11 November 2010
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
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
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
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
can you attach an example xml where it does not work as well?
  11 November 2010
<curveShape name="circle">
<code codeString="some code" orientation="ZY"/>
  11 November 2010
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.
Ok, But Can You Do It...ON FIRE!
  11 November 2010
Thread automatically closed

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.
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
Thread Closed share thread

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Society of Digital Artists

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump

All times are GMT. The time now is 02:58 AM.

Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.