faqts : Computers : Programming : Languages : Python : Common Problems : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 9 people (89%) answered Yes
Recently 4 of 4 people (100%) answered Yes

Entry

Where can I find some kind of helping material (tutorials etc) about how to use XML and Python?

Mar 22nd, 2002 10:03
Joel Lawhead, Fred Drake, unknown unknown, Boudewijn Rempt, Harry George


Take a look at the Python/XML HOW-TOs and related documentation,
located at:

http://www.python.org/topics/xml/docs.html

--------------------

Have a look here:

http://www.valdyas.org/python/index.html

dbobj - the repository is XML based
kura  - webserver that takes XML templates and spews out HTML, and an
        XML data import utility.
kxml  - a small PyKDE xml file browser (unfinished!)

A really simplistic reader/writer for Abiword.  Available at:
  http://www.seanet.com/~hgg9140/comp/index.html

--------------------

Install PyXML, then try this.  Read the xml.dom.core for element names
and the basic functions.  If you are not familiar with xml itself, see
the XML Bible.  Try this routine on some known-good .xml file.

#!/usr/bin/env python
#/* helloxml.py 

"""
Usage: helloxml.py [options] filename
E.g.,  helloxml.py myfile
Options
-h,--help        this message
-v,--version     version

Revisions:
2000-02-23  Harry George   initial version
"""

import sys,os,getopt,time,re,string,exceptions

modname="helloxml"
__version__="0.1"

def debug(ftn,txt):
        sys.stdout.write(modname+'.'+ftn+':'+txt+'\n')
        sys.stdout.flush()

def fatal(ftn,txt):
        sys.stdout.write(modname+'.'+ftn+':FATAL:'+txt+'\n')
        sys.stdout.flush()
        sys.exit(1)

def usage():
        print __doc__

#====================================
from xml.dom import core, utils

def main():
        ftn = "main"
        pargs=[]    #positional args, default is empty
        opts,pargs=getopt.getopt(sys.argv[1:],'hv',['help','version'])
        for opt in opts:
                if opt[0]=='-h' or opt[0]=='--help':
                        usage()
                        sys.exit(0)
                elif opt[0]=='-v' or opt[0]=='--version':
                        print modname+": version="+__version__
                        sys.exit(0)

        #---get the DOM "doc"---
        if len(pargs)==0:
                        usage()
                        sys.exit(0)             
        filename = pargs[0]
        doc = utils.FileReader( filename ).document

        #---dump it out---
        doc.dump();

        #---convert DOM tree back to XML, and dump that
        xml = doc.toxml()
        print xml

        #---dump the doc manually---
        root=doc.get_documentElement()  
        mydump(root)

def mydump(node):
        ftn = "mydump"
        debug(ftn,"name=" +node.get_nodeName())
        #---dump attributes---
        attrs=node.get_attributes()
        keys=attrs.keys()
        for attr in keys:
                debug(ftn,"attr="+attr+" 
value="+attrs[attr].get_nodeValue())
        #---dump children---
        children=node.get_childNodes()
        for child in children:
        #--report only elements
                if child.get_nodeType()==core.ELEMENT: 
                        mydump(child)
                         
if __name__ == '__main__': main()
-------------------------------------------
One of the best quick beginning tutorials I've found on using Python 
and XML is "Dive into Python" which teaches you to use the very 
simple "minidom" module:

http://diveintopython.org/kgp_divein.html