Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python, Lua, Tcl, C and C++ training - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, Ruby, MySQL and Linux / Tomcat systems
 
XML handling in Python - a new teaching example using etree

From the Python course just completed - a new example of XML handling, through the xml.etree.ElementTree module which was first available in Python 2.5.

  import xml.etree.ElementTree as etree
  try:
    tree = etree.parse(args.sourcefile[0])
  except Exception as e:
    print('FAILED to parse - reason %s' % e)
    exit(2)


An exception is thrown if the file cannot be opened, or if it can but contains badly formed XML, for example:

  munchkin:cambx grahamellis$ ./xmlparserdemo 6_context.xml 
  FAILED to parse - reason not well-formed (invalid token): line 3, column 1


Run with a properly formed and readable XML file, you can then iterate through the tree, and indeed do so recuresively if you wish:

  root = tree.getroot()
  for thing in root:
    print(thing)
    print(thing.attrib)
    print(thing.tag)
    print(thing.text)
    kids = thing.getchildren()
    print(kids)
    print("------------------")


Here's a sample of the results you may get (from a very short piece of XML in this case!)

  munchkin:cambx grahamellis$ ./xmlparserdemo -r 6_context.xml 
  <Element 'WatchedResource' at 0x10a037e10>
  {}
  WatchedResource
  WEB-INF/web.xml
  []
  ------------------


That's a single tag within the root, with no attributes (hence the empty dict) and no children (hence the empty list).

Complete program using this code [here] ... the program starts with an example of command line handling - the sample XML file I used is [here]. It also includes code to explore all elements of the tree ... Finally, the xml.etree.ElementTree module also includes tree searchimg and amendment methods.
(written 2015-12-09)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y151 - Python & XML
  [4710] Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)
  [3082] XML handling in Python - SAX, DOM and XSLT examples - (2010-12-09)
  [2555] Bookkeeping - (2009-12-29)
  [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18)
  [2378] Handling XML in Perl - introduction and early examples - (2009-08-27)


Back to
Command line parameter handling in Python via the argparse module
Previous and next
or
Horse's mouth home
Forward to
Python formatting update - including named completions
Some other Articles
21 places to get your train and bus timetables for TransWilts
Not standing as your local councillor
Call for help counting passengers - TransWilts, 12th to 14th December
Python formatting update - including named completions
XML handling in Python - a new teaching example using etree
Command line parameter handling in Python via the argparse module
A comparison of the public transport alternatives to the Royal United Hospital, Bath - from Melksham.
From single block to structure and object oriented programming
Progress on moving from Python 2 to Python 3 - training for both versions
Principles or a GUI and their practical application using wxPthon
4755 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2017: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4594_XML ... etree.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat