XMLObject - problem with recursive definitions

M

Michael Foord

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
 
R

rzed

(e-mail address removed) (Michael Foord) wrote in

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 copied your code, installed XMLObject (thanks for mentioning
this, by the way -- I had not been aware of it before), and ran the
program, which worked fine.

Output:
<?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"/>
</thing>


.... but this was based on the latest XMLObject. Maybe it's been
patched in response to your message?
 
M

Michael Foord

Apparently this has already been fixed - and a new release will be
made in the next couple of days with this fixed....

I'm just surprised it wasn't my fault :)

Fuzzy
 
M

Michael Foord

rzed said:
<?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>


... but this was based on the latest XMLObject. Maybe it's been
patched in response to your message?



Yeah - the new release 0.1.2 corrects the problem.
It's the first XML library I've used. I'm using it for both reading
and writing data structures as XML rather than a general XML
parser...... but it's very easy to use.

Regards,


Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top