For 2023 - we are now fully retired from IT training. We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.
Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!
I am also active in many other area and still look after a lot of web sites - you can find an index ((here)) |
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 [2378] Handling XML in Perl - introduction and early examples - (2009-08-27) [2506] Good example of recursion in Python - analyse an RSS feed - (2009-11-18) [2555] Bookkeeping - (2009-12-29) [3082] XML handling in Python - SAX, DOM and XSLT examples - (2010-12-09) [4710] Searching a Json or XML structure for a specific key / value pair in Python - (2016-10-30)
Some other Articles
21 places to get your train and bus timetables for TransWiltsNot standing as your local councillorCall for help counting passengers - TransWilts, 12th to 14th DecemberPython formatting update - including named completionsXML handling in Python - a new teaching example using etreeCommand line parameter handling in Python via the argparse moduleA comparison of the public transport alternatives to the Royal United Hospital, Bath - from Melksham.From single block to structure and object oriented programmingProgress on moving from Python 2 to Python 3 - training for both versionsPrinciples or a GUI and their practical application using wxPthon
|
4759 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).
|
|