Differences between revisions 1 and 2
Revision 1 as of 2004-05-30 22:07:32
Size: 1151
Editor: dsl254-010-130
Comment: simple XML parsing in Python2.3 with minidom
Revision 2 as of 2005-02-08 01:56:55
Size: 1433
Editor: 168-103-146-113
Comment: links to info about minidom
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:

== Links ==

 * [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

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>" )

Then you can do things like:

   1 dir( dom2 ) # get a list of things to do
   2 
   3 print dom2.toxml() # prints out "<?xml version="1.0" ?>..."
   4 print dom2.nodeName # will return "#document"
   5 print dom2.firstChild.nodeName # will return "myxml" because it's <myxml>...
   6 print dom2.firstChild.nodeValue # it's None! (everything's in the children.)
   7 print dom2.firstChild.hasChildNodes() # it's True.
   8 print dom2.firstChild.firstChild.nodeName # will return "#text"
   9 print dom2.firstChild.firstChild.nodeValue # will return "Some data"
  10 print dom2.firstChild.firstChild.nextSibling.nodeName # will return "empty" because it's <empty/>
  11 print dom2.firstChild.firstChild.nextSibling.nodeValue # it's None!
  12 print dom2.firstChild.firstChild.nextSibling.nextSibling.nodeName # will return "#text"
  13 print dom2.firstChild.firstChild.nextSibling.nextSibling.nodeValue # will return " some mode data"

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

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