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

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

Modifying the DOM tree

How to make changes in an already existing DOM tree.
The following code shows how to move parts of the DOM tree around from one place to another.
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: (dom4-1.py)
  1   import xml.dom.minidom as dom
  2   
  3   doc = dom.parse( "example.xml")
  4   
  5   # all example elements
  6   examples = doc.getElementsByTagName( "example")
  7   example1 = examples[0]
  8   example2 = examples[1]
  9   
 10   # modify the attributes
 11   example1.setAttribute( "num", "XXX")
 12   example2.setAttribute( "something", "else")
 13   
 14   # get the title element that is child of example1
 15   title1 = [ch for ch in example1.childNodes if ch.nodeName == "title"][0]
 16   
 17   # remove the title from example1
 18   example1.removeChild( title1)
 19   # and append it to example2
 20   example2.appendChild( title1)
 21   
 22   # print out the xml
 23   print doc.toxml()
stdout:
<?xml version="1.0" ?>
<examples>
  <example num="XXX">
    
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2" something="else">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  <title>Example 1</title></example>
</examples>
Doba běhu: 50.4 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: (dom4-2.py)
  1   import xml.dom.minidom as dom
  2   
  3   doc = dom.parse( "example.xml")
  4   
  5   # all example elements
  6   examples = doc.getElementsByTagName( "example")
  7   example1 = examples[0]
  8   example2 = examples[1]
  9   
 10   # get the title element that is child of example1
 11   title1 = [ch for ch in example1.childNodes if ch.nodeName == "title"][0]
 12   # get the title element that is child of example2
 13   title2 = [ch for ch in example2.childNodes if ch.nodeName == "title"][0]
 14   
 15   # remove the title from example1
 16   example1.insertBefore( title2, title1)
 17   
 18   # print out the xml
 19   print doc.toxml()
stdout:
<?xml version="1.0" ?>
<examples>
  <example num="1">
    <title>Example 2</title><title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example num="2">
    
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Doba běhu: 50.5 ms
Please note that once an element is added to a new parent, it is automatically removed from the old one.
The following code fixes this.
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: (dom4-3.py)
  1   import xml.dom.minidom as dom
  2   
  3   doc = dom.parse( "example.xml")
  4   
  5   # all example elements
  6   examples = doc.getElementsByTagName( "example")
  7   example1 = examples[0]
  8   example2 = examples[1]
  9   
 10   # get the title element that is child of example1
 11   title1 = [ch for ch in example1.childNodes if ch.nodeName == "title"][0]
 12   # get the title element that is child of example2
 13   title2 = [ch for ch in example2.childNodes if ch.nodeName == "title"][0]
 14   # clone the title2
 15   title3 = title2.cloneNode( False)  # deep=False, only copy the element
 16   title4 = title2.cloneNode( True)   # deep=True, copy the whole sub-tree
 17   
 18   # remove the title from example1
 19   example1.insertBefore( title3, title1)
 20   example1.appendChild( title4)
 21   
 22   # print out the xml
 23   print doc.toxml()
stdout:
<?xml version="1.0" ?>
<examples>
  <example num="1">
    <title/><title>Example 1</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  <title>Example 2</title></example>

  <example num="2">
    <title>Example 2</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Doba běhu: 49.9 ms