Created 25 January 2004
Handyxml is a Python module that makes simple XML use convenient. It provides Python attributes for XML attributes and child elements, and provides a facade over two common XML parsers and an XPath implementation. It’s designed for casual XML use in environments such as template engines where data is stored in XML files.
Imagine a simple XML file:
<!-- MyDataFile.xml -->
<property name='duck1' value='huey' />
<property name='duck2' value='louie' />
<property name='duck3' value='dewey' />
To parse the file, simply use the
>> import handyxml
>> doc = handyxml.xml('MyDataFile.xml')
It accepts either a filename or an open file as its argument, and parses the XML it finds. Its return value is the document element, and attributes on the element are available as attributes on the object:
The names of child elements are object attributes with lists of the child elements:
Unlike other Python XML-to-dictionary schemes, these objects act just like full-fledged DOM nodes, and so can be processed with other XML facilities.
Handyxml also provides the
xpath function for evaluating
XPath expressions. The first argument is either an argument acceptable to xml(),
or a parsed XML node.
The second argument is the XPath expression to evaluate against the XML.
The return is the resulting list of XML nodes:
>> duck3 = handyxml.xpath(doc, '//property[@name="duck3"]')
When looking for a file, handyxml will search the list of directories
specified in its
path value, much like
27 January 2004: The xml.xpath module is no longer required, and the xpath() function will raise an exception if the module couldn’t be imported. The xml() function can now take either a string (filename) or an open file as its argument.
Would not it be nicer to have
from xml import xpath as ...
but that raises an ImportError in my Python 2.3.3 version. Is xml.xpath from a 3rd party?
Any other opinions?
I get ImportError: No module named ext.reader
I just have the standard python instilation nothing special. Am I missing a module?
But how can I get CDATA?
I am working on a console program that will load an xml file, and then allow repeated user input to work with that XML. I want the user to be able to make changes to the XML file on disk, and then reload it without having to exit the program. The built-in cache in handyxml prevented this.
I just added a second argument to the xml function: xml(xmlin, cache=True), and then whenever the _xmlcache is accessed it checks the cache flag first. This way I can specify that a particular file not be cached.
Not sure if this would be useful to others, but thought I would pass it along in case you were still updating this script.
Since PyXML is not available anymore, how to use handyxml and Cog using currently available tools?
Which modules to install and which modifications to make?
Add a comment: