Python-Ref > XML > DOM > Reading the DOM tree
 
 

<-^^->
Moduly
Knihovní funkce

Reading the DOM tree

How to get information from a DOM tree.
Expand/Shrink
<examples>
  <example num="1">
    <title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (dom2-1.py)
  1   import xml.dom.minidom as dom  # this saves us some typing later
  2   
  3   doc = dom.parse( "example.xml")
  4   print doc  # the document, not the root element
  5   
  6   for child in doc.childNodes:
  7       print child   # the root element is in fact the only child of the document
  8   
  9       for child2 in child.childNodes:
 10           print " ", child2  # the child-nodes of the root element
 11   
 12           for child3 in child2.childNodes:
 13               print "   ", child3
stdout:
<xml.dom.minidom.Document instance at 0x2aea6c252950>
<DOM Element: examples at 0x2aea6c252bd8>
  <DOM Text node "
  ">
  <DOM Element: example at 0x2aea6c252d88>
    <DOM Text node "
    ">
    <DOM Element: title at 0x2aea6c253a28>
    <DOM Text node "
    ">
    <DOM Element: text at 0x2aea6c253320>
    <DOM Text node "
  ">
  <DOM Text node "

  ">
  <DOM Element: example at 0x2aea6c253518>
    <DOM Text node "
    ">
    <DOM Element: title at 0x2aea6c2537e8>
    <DOM Text node "
    ">
    <DOM Element: text at 0x2aea6c2542d8>
    <DOM Text node "
  ">
  <DOM Text node "
">
Doba běhu: 49.7 ms
Expand/Shrink
<examples>
  <example num="1">
    <title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (dom2-2.py)
  1   import xml.dom.minidom as dom
  2   
  3   def print_element( e, level=0):
  4       print level*" ",
  5       if isinstance( e, dom.Text):
  6           print "text:", e.data.strip()
  7       else:
  8           print e.nodeName
  9           for ch in e.childNodes:
 10               print_element( ch, level=level+2)
 11   
 12   
 13   doc = dom.parse( "example.xml")
 14   
 15   print_element( doc.childNodes[0])
stdout:
 examples
   text: 
   example
     text: 
     title
       text: Example 1
     text: 
     text
       text: This is example nr. 1. It shows how an example looks.
     text: 
   text: 
   example
     text: 
     title
       text: Example 2
     text: 
     text
       text: Another example. Imagine some ingenious text here...
     text: 
   text: 
Doba běhu: 50.0 ms
Expand/Shrink
<examples>
  <example num="1">
    <title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (dom2-3.py)
  1   import xml.dom.minidom as dom
  2   
  3   def print_element( e, level=0):
  4       if isinstance( e, dom.Element):
  5           print level*" ",
  6           print e.nodeName
  7           for i in range( e.attributes.length):  # we iterate over all..
  8               a = e.attributes.item( i)          # ..attributes 
  9               print (level+2)*" ", "@%s = %s" % (a.name, a.value)
 10           for ch in e.childNodes:
 11               print_element( ch, level=level+2)
 12   
 13   
 14   doc = dom.parse( "example.xml")
 15   
 16   print_element( doc.childNodes[0])
stdout:
 examples
   example
     @num = 1
     title
     text
   example
     @num = 2
     title
     text
Doba běhu: 50.1 ms
Expand/Shrink
<examples>
  <example num="1">
    <title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (dom2-4.py)
  1   import xml.dom.minidom as dom
  2   
  3   def print_element( e, level=0):
  4       if isinstance( e, dom.Element):
  5           print level*" ",
  6           print e.nodeName
  7           if e.getAttribute( "num") != "":  # we ask for a specific attribute
  8               print (level+2)*" ", "@num = %s" % e.getAttribute( "num")
  9           for ch in e.childNodes:
 10               print_element( ch, level=level+2)
 11   
 12   
 13   doc = dom.parse( "example.xml")
 14   
 15   print_element( doc.childNodes[0])
stdout:
 examples
   example
     @num = 1
     title
     text
   example
     @num = 2
     title
     text
Doba běhu: 50.3 ms