XMLObject - problem with recursive definitions

Michael Foord fuzzyman at gmail.com
Mon Aug 23 03:23:35 EDT 2004


I've been using the excellent XMLObject and have unfortunately come up
against what *looks* to me like a bug - although it's very possible
that the problem is  mine  !!

I want to use XML object to create an XML structure that represents
the contents of a directory structure. I call it a DirObj. Each DirObj
needs a list node that can itself contaibn several more DirObj -
effectively a 'recursive' definition.

It looks something like this :

class XMLDirObj(XMLObject):
    """An XMLObject version of a DirObj."""
    attrs = ItemNode('DirAttributes')
    files = ListNode('XMLFileObj')
    dirs = ListNode('XMLDirObj')

A DirAttributes object is an object representing the set of attributes
of the directory, an XMLFileObj is an object representing the set of
attributes of each file in the directory. Unfortunately when I use the
toXml and then the (classmethod) fromXml it mangles the values. (on
the read).

I've written a small test script with the simplest possible recursive
use of XMLObject and it doesn't appear to work. I wondered if this was
actually a bug - or whether I was doing something wrong. My test
script is below and from the sample output you'll see the error and
the assert failure at the bottom. The first print is ok (toXml) - but
then it reads it incorrectly (the second print).

Ouput :

<?xml version="1.0" encoding="iso-8859-1" ?>
<thing abit="Hello My Friend">
  <thing abit="Recursion Test">
    <anotherthing abit="Yo Again"/>
  </thing>
  <anotherthing abit="Yo"/>
</thing>


<?xml version="1.0" encoding="iso-8859-1" ?>
<thing abit="Hello My Friend">
  <thing abit="Recursion Test">
    <anotherthing abit="Yo Again"/>
  </thing>
  <anotherthing abit="Yo Again"/>
</thing>
Traceback (most recent call last):
  File "D:\Python Projects\dirwatcher\rec_test.py", line 67, in ?
    assert thestuff == thestuff2
AssertionError


####################


"""
rec_test.py

Demonstrates that recursion fails using XMLObject

:-(

"""


from XMLObject import *


########################################################################


class Thing(XMLObject):
    place = ItemNode('Anotherthing')
    anotherplace = ListNode('Thing')
    abit = CDATAttribute(optional=True)

class Anotherthing(XMLObject):
    abit = CDATAttribute(optional=True)


########################################################################
# Test Function

if __name__=='__main__':
    test1 = Thing()
    test1.abit = 'Hello My Friend'
    athing = Anotherthing()
    athing.abit = 'Yo'
    test1.place = athing
   
    test2 = Thing()
    test2.abit = 'Recursion Test'
    athing2 = Anotherthing()
    athing2.abit = 'Yo Again'
    test2.place = athing2
   
    test1.anotherplace.append(test2)
   
    thestuff =  test1.toXml()
    print thestuff
   
    testobj = Thing.fromXml(thestuff)
    thestuff2 = testobj.toXml()
   
    print
    print
    print thestuff2
   
    assert thestuff == thestuff2
#####################################

If anyone can see what I've done wrong it would be much appreciated.
(I've emailed the author - but as it's probably my fault I thought I'd
post here as well).


Regards, 

Fuzzy


    

-- 

http://www.Voidspace.org.uk 
The Place where headspace meets cyberspace. Online resource site -
covering science, technology, computing, cyberpunk, psychology,
spirituality, fiction and more.

---
http://www.Voidspace.org.uk/atlantibots/pythonutils.html
Python utilities, modules and apps.
Including Nanagram, Dirwatcher and more.
---
http://www.fuchsiashockz.co.uk   
http://groups.yahoo.com/group/void-shockz
---

Everyone has talent. What is rare is the courage to follow talent
 to the dark place where it leads. -Erica Jong
Ambition is a poor excuse for not having sense enough to be lazy.
         -Milan Kundera



More information about the Python-list mailing list