create a XML file with DOM

J

Juliano Freitas

Hello!

i'm usin a DOM module to process a xml file. I'm having problems with
the appenChild function. Why in my example below i can't append the
child 'ele' into the swdb.

<swdb>
<visualinfo/>
</swdb>

Juliano Freitas

=============================================
from xml.dom import *

xmldoc = getDOMImplementation()
print xmldoc

root = xmldoc.createDocument('', 'swdb', '')
root.firstChild

ele = root.createElement('visualinfo')
root.appendChild(ele)

swdb = root.toprettyxml()
print swdb
=============================================

Traceback (most recent call last):
File "/home/jubafre/teste_dom2.py", line 10, in -toplevel-
root.appendChild(root.createElement('visualinfo'))
File "/usr/lib/python2.3/xml/dom/minidom.py", line 1552, in
appendChild
raise xml.dom.HierarchyRequestErr(
HierarchyRequestErr: two document elements disallowed
 
P

Peter Hansen

Juliano said:
i'm usin a DOM module to process a xml file. I'm having problems with
the appenChild function. Why in my example below i can't append the
child 'ele' into the swdb.

Does the following exploration help you? Note the difference between
what is output by root.toprettyxml() and root.firstChild.toprettyxml().
You need to go back to the XML specification and read up a little
bit about the difference between the "root" and the "document element".

c:\>python
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\a\python23\lib\xml\dom\minidom.py", line 1552, in appendChild
raise xml.dom.HierarchyRequestErr(
xml.dom.HierarchyRequestErr: two document elements disallowed'<?xml version="1.0" ?>\n<swdb>\n\t<visualinfo/>\n</swdb>\n'


-Peter
 

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,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top