Python-Ref > XML > DOM > Building a DOM tree
 
 

<-^^
Moduly
Knihovní funkce

Building a DOM tree

How to build a DOM tree from scratch.
Building a new XML document via DOM is especially tedious job, especially in simple cases, where the overhead of the DOM commands is most obvious. On the other hand in more complex environments, the usage of DOM over simple print statements is in the fact that it automatically ensures a well-formed document will be produced.
The first two examples on this page compare the length of code needed to create an XML document using DOM and print.
Expand/Shrink
Prostředí:
.
|-- dom5-1.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (dom5-1.py)
  1   import os
  2   import xml.dom.minidom as dom
  3   
  4   doc = dom.Document()
  5   root = doc.createElement( "tree")
  6   doc.appendChild( root)
  7   
  8   for f in os.listdir( "./"):
  9       if os.path.isdir( f):
 10           e = doc.createElement( "directory")
 11       elif os.path.isfile( f):
 12           e = doc.createElement( "file")
 13           e.setAttribute( "size", str( os.path.getsize( f))) # must be str
 14       e.setAttribute( "name", f)
 15       root.appendChild( e)
 16   
 17   print doc.toxml()
stdout:
<?xml version="1.0" ?>
<tree><file name="dom5-1.py" size="434"/><directory name="something"/><directory name="something_else"/><directory name="test"/></tree>
Doba běhu: 60.1 ms
Expand/Shrink
Prostředí:
.
|-- dom5-2.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (dom5-2.py)
  1   import os
  2   
  3   print "<tree>"
  4   
  5   for f in os.listdir( "./"):
  6       if os.path.isdir( f):
  7           print '<directory name="%s"/>' % f
  8       elif os.path.isfile( f):
  9           print '<file name="%s" size="%d"/>' % (f, os.path.getsize( f))
 10   
 11   print "</tree>"
stdout:
<tree>
<file name="dom5-2.py" size="241"/>
<directory name="something"/>
<directory name="something_else"/>
<directory name="test"/>
</tree>
Doba běhu: 33.2 ms
The code below shows how to create text nodes.
Expand/Shrink
Prostředí:
.
|-- dom5-3.py
|-- something
|   `-- example.py
|-- something_else
`-- test
    `-- test.txt
Zdroj: (dom5-3.py)
  1   import os
  2   import xml.dom.minidom as dom
  3   
  4   doc = dom.Document()
  5   root = doc.createElement( "tree")
  6   doc.appendChild( root)
  7   
  8   for f in os.listdir( "./"):
  9       if os.path.isdir( f):
 10           e = doc.createElement( "directory")
 11       elif os.path.isfile( f):
 12           e = doc.createElement( "file")
 13           e.setAttribute( "size", str( os.path.getsize( f))) # must be str
 14       text = doc.createTextNode( f)
 15       e.appendChild( text)
 16       root.appendChild( e)
 17       # for prettier output
 18       root.appendChild( doc.createTextNode( "\n"))
 19   
 20   print doc.toxml()
stdout:
<?xml version="1.0" ?>
<tree><file size="537">dom5-3.py</file>
<directory>something</directory>
<directory>something_else</directory>
<directory>test</directory>
</tree>
Doba běhu: 61.4 ms