Differences between revisions 2 and 15 (spanning 13 versions)
Revision 2 as of 2005-02-08 01:56:55
Size: 1433
Editor: 168-103-146-113
Comment: links to info about minidom
Revision 15 as of 2012-03-07 11:43:42
Size: 2998
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
'''NOTE''': Some people think that MiniDOM is a slow and very memory hungry DOM implementation. According to these people, if you are looking for a fast, memory efficient and simple to use tool for working with XML, try ElementTree instead (in the [[http://docs.python.org/library/xml.etree.elementtree.html | xml.etree]] package), or use the external [[http://lxml.de/ | lxml]] implementation.
Line 9: Line 11:
print dom1.toxml()
print dom2.toxml()
Line 11: Line 15:
Then you can do things like: == Examples of Use ==

 * node.nodeName
 * node.nodeValue
 * node.childNodes

=== Find Elements ===

You can manually walk through the {{{childNodes}}} tree, comparing {{{nodeName}}}s.

You might be able to use {{{getElementsByTagName}}} as well:
Line 15: Line 29:
dir( dom2 ) # get a list of things to do from xml.dom.minidom import parse
dom = parse("foo.xml")
for node in dom.getElementsByTagName('bar'): # visit every node <bar />
    print node.toxml()
}}}
Line 17: Line 35:
print dom2.toxml() # prints out "<?xml version="1.0" ?>..."
print dom2.nodeName # will return "#document"
print dom2.firstChild.nodeName # will return "myxml" because it's <myxml>...
print dom2.firstChild.nodeValue # it's None! (everything's in the children.)
print dom2.firstChild.hasChildNodes() # it's True.
print dom2.firstChild.firstChild.nodeName # will return "#text"
print dom2.firstChild.firstChild.nodeValue # will return "Some data"
print dom2.firstChild.firstChild.nextSibling.nodeName # will return "empty" because it's <empty/>
print dom2.firstChild.firstChild.nextSibling.nodeValue # it's None!
print dom2.firstChild.firstChild.nextSibling.nextSibling.nodeName # will return "#text"
print dom2.firstChild.firstChild.nextSibling.nextSibling.nodeValue # will return " some mode data"
{{{getElementsByTagName}}} finds all children of a given name, no matter how deep, thus working recursively. This is usually good, but can cause problems if similar nodes exist at multiple levels and the intervening nodes are important.

=== Add an Element ===

Create & add an XML element (Something like {{{<foo />}}}) to an XML document.

{{{
#!python
from xml.dom.minidom import parse
dom = parse("bar.xml")
x = dom.createElement("foo") # creates <foo />
dom.childNodes[1].appendChild(x) # appends at end of 1st child's children
print dom.toxml()
}}}

=== Add an Element with Text Inside ===

Create & add an XML element to an XML document, the element has text inside.

ex: {{{<foo>hello, world!</foo>}}}

{{{
#!python
from xml.dom.minidom import parse
dom = parse("bar.xml")
x = dom.createElement("foo") # creates <foo />
txt = dom.createTextNode("hello, world!") # creates "hello, world!"
x.appendChild(txt) # results in <foo>hello, world!</foo>
dom.childNodes[1].appendChild(x) # appends at end of 1st child's children
print dom.toxml()
}}}

=== Import a Node ===

You can use DOM 2 "importNode" to take part of one XML document, and put it into another XML document.

{{{
#!python
from xml.dom.minidom import parse
dom1 = parse("foo.xml")
dom2 = parse("bar.xml")
x = dom1.importNode(dom2.childNodes[1], # take 2nd node in "bar.xml"
                    True) # deep copy
dom1.childNodes[1].appendChild(x) # append to children of 2nd node in "foo.xml"
print dom1.toxml()
Line 32: Line 84:
 * [http://www.python.org/doc/2.3.2/lib/module-xml.dom.minidom.html Python 13.7, xml.dom.minidom] -- API reference
 * [http://www.
faqs.org/docs/diveintopython/kgp_divein.html#kgp.divein Dive into Python, Chapter 5] -- works almost entirely out of the minidom API
 * [[http://docs.python.org/lib/module-xml.dom.minidom.html|Python Library Reference, xml.dom.minidom]] -- API documentation
 * [
[http://www.faqs.org/docs/diveintopython/kgp_divein.html#kgp.divein|Dive into Python, Chapter 5]] -- works almost entirely out of the minidom API

== See Also ==

 * [[Sax]], PythonXml

NOTE: Some people think that MiniDOM is a slow and very memory hungry DOM implementation. According to these people, if you are looking for a fast, memory efficient and simple to use tool for working with XML, try ElementTree instead (in the xml.etree package), or use the external lxml implementation.

Some notes on how to use xml.dom.minidom:

   1 from xml.dom.minidom import parse, parseString
   2 
   3 dom1 = parse( "foaf.rdf" )   # parse an XML file
   4 dom2 = parseString( "<myxml>Some data <empty/> some more data</myxml>" )
   5 print dom1.toxml()
   6 print dom2.toxml()

Examples of Use

  • node.nodeName
  • node.nodeValue
  • node.childNodes

Find Elements

You can manually walk through the childNodes tree, comparing nodeNames.

You might be able to use getElementsByTagName as well:

   1 from xml.dom.minidom import parse
   2 dom = parse("foo.xml")
   3 for node in dom.getElementsByTagName('bar'):  # visit every node <bar />
   4     print node.toxml()

getElementsByTagName finds all children of a given name, no matter how deep, thus working recursively. This is usually good, but can cause problems if similar nodes exist at multiple levels and the intervening nodes are important.

Add an Element

Create & add an XML element (Something like <foo />) to an XML document.

   1 from xml.dom.minidom import parse
   2 dom = parse("bar.xml")
   3 x = dom.createElement("foo")  # creates <foo />
   4 dom.childNodes[1].appendChild(x)  # appends at end of 1st child's children
   5 print dom.toxml()

Add an Element with Text Inside

Create & add an XML element to an XML document, the element has text inside.

ex: <foo>hello, world!</foo>

   1 from xml.dom.minidom import parse
   2 dom = parse("bar.xml")
   3 x = dom.createElement("foo")  # creates <foo />
   4 txt = dom.createTextNode("hello, world!")  # creates "hello, world!"
   5 x.appendChild(txt)  # results in <foo>hello, world!</foo>
   6 dom.childNodes[1].appendChild(x)  # appends at end of 1st child's children
   7 print dom.toxml()

Import a Node

You can use DOM 2 "importNode" to take part of one XML document, and put it into another XML document.

   1 from xml.dom.minidom import parse
   2 dom1 = parse("foo.xml")
   3 dom2 = parse("bar.xml")
   4 x = dom1.importNode(dom2.childNodes[1],  # take 2nd node in "bar.xml"
   5                     True)  # deep copy
   6 dom1.childNodes[1].appendChild(x)  # append to children of 2nd node in "foo.xml"
   7 print dom1.toxml()

See Also

MiniDom (last edited 2012-03-07 11:43:42 by MartinvonLoewis)

Unable to edit the page? See the FrontPage for instructions.