[XML-SIG] To .document or not to .document
Randall Hopper
aa8vb@yahoo.com
Fri, 10 Dec 1999 13:06:59 -0500
--DocE+STaALJfprDB
Content-Type: text/plain; charset=us-ascii
When xml.dom.utils.FileReader is used to read in an XML DOM, the DOM may or
may not have a leading .document component depending on whether the XML was
read from a file or a stream.
Why the difference?
For example, the attached script reads a simple XML script from the XML
variable via StringIO. If you change:
reader = utils.FileReader().readStream( stream )
to:
reader = utils.FileReader( "elements.xml" )
then the script doesn't work unless you change:
doc = reader
to:
doc = reader.document
There's an extra DOM component here.
Thanks for any insight.
--
Randall Hopper
aa8vb@yahoo.com
--DocE+STaALJfprDB
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="elements.py"
#!/usr/bin/env python
from xml.dom import utils,core
import string, sys, StringIO
XML = """\
<?xml version="1.0"?>
<root>
<element1/>
<element2/>
<element3/>
</root>
"""
stream = StringIO.StringIO( XML )
reader = utils.FileReader().readStream( stream )
doc = reader
rootNode = doc.documentElement
rootNode2 = doc.childNodes.item(0)
print rootNode
print rootNode2
element1 = rootNode.childNodes.item(0)
element2 = rootNode.childNodes.item(1)
element3 = rootNode.childNodes.item(2)
print element1, element2, element3
--DocE+STaALJfprDB
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="elements.xml"
<?xml version="1.0"?>
<root>
<element1/>
<element2/>
<element3/>
</root>
--DocE+STaALJfprDB--