[Tutor] XML DOM

Kuros kuros at sbcglobal.net
Sun Aug 24 21:52:18 EDT 2003


Hello,

I'm running into a problem using DOM with XML.

What I want to do is this:

I have an XML document, it looks like this:

<world>
 <room>
  <id>1</id>
  <name>A Room</name>
  <desc>A Large Room</desc>
  a few other values like these..
 </room>
 <room>
 a new room with the same values here
 </room>
</world>

Now, what I want to do is use DOM to go over ever room entry, and create a new class containing the values.

I have my class looking like this:

class Room:
     id = 0
     name = ''
     desc = ''
and more values for every attribute in the XML document

Now, I want to take all the attributes in the room node of the XML document, and assign them to thier appropiate value in the object. At the end of the room node, I want to create a new class for the room, and add it to a list of room classes. Repeat for every room node.

That I think I know how to do, but what I am having trouble with is traversing the DOM tree to get the values.

I have managed to create a treewalker, and get the DOM loaded into memory. I can make it display a list of node names and thier values, with something like this...

while 1:
print walker.currentNode.nodeName
print walker.currentNode.nodeValue
next = walker.nextNode()
if next is None: break

I have tried to do something like this:

while 1:
next = walker.nextNode()
if walker.currentNode.nodeName == 'room':
curnode = next.currentNode.firstChild
roomid = curnode.nodeValue
curnode = curnode.nextSibling
etc

Unfortuantly, it doesn't work. The interperter window just continually scrolls...

If anyone has a link to something that could help me, or any ideas on how to accomplish this, I would be most thankful.

Thanks,
Kuros
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20030824/e605a0a7/attachment.htm


More information about the Tutor mailing list