Python-Ref > XML > DOM > Cherrypicking parts of a DOM tree
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Cherrypicking parts of a DOM tree

How to quickly find an element without browsing through all the elements.
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: (dom3-1.py)
  1   import xml.dom.minidom as dom
  2   
  3   doc = dom.parse( "example.xml")
  4   
  5   # getElementsByTagName find all the elements with a specified name,
  6   # no matter how deep in the document they are buried
  7   for e in doc.getElementsByTagName( "title"):
  8       print e.nodeName
  9       print e.childNodes
stdout:
title
[<DOM Text node "Example 1">]
title
[<DOM Text node "Example 2">]
Doba běhu: 49.7 ms