The lxml.objectify sub-package is extremely handy for parsing and creating XML. In this article, we will show how to create XML using the lxml package. We’ll start with some simple XML and then try to replicate it. Let’s get started! (more…)
Entries tagged with “XML Parsing Series”.
Wed 26 Mar 2014
Tue 30 Apr 2013
If you have followed this blog for a while, you may remember that we’ve covered several XML parsing libraries that are included with Python. In this article, we’ll be continuing that series by taking a quick look at the ElementTree library. You will learn how to create an XML file, edit XML and parse the XML. For comparison’s sake, we’ll use the same XML we used in the previous minidom article to illustrate the differences between using minidom and ElementTree. Here is the original XML:
< ?xml version="1.0" ?> <zappointments reminder="15"> <appointment> <begin>1181251680</begin> <uid>040000008200E000</uid> <alarmtime>1181572063</alarmtime> <state></state> <location></location> <duration>1800</duration> <subject>Bring pizza home</subject> </appointment> </zappointments>
Now let’s dig into the Python! (more…)
Wed 18 Jul 2012
Note: The following post was originally published over on Dzone. I changed the title because I already wrote several XML parsing articles and don’t want my readers to get this one confused with the others.
One of the common tasks I am given in my day job is to take some data format input and parse it to create a report or some other document. Today we’ll look at taking some XML input, parsing it with the Python programming language and then creating a letter in PDF format using Reportlab, a 3rd party package for Python. Let’s say my company receives an order for three items that I need to fulfill. The XML for that could look like the following code: (more…)
Wed 6 Jun 2012
A couple years ago I started a series of articles on XML parsing. I covered lxml’s etree and Python’s included minidom XML parsing library. For whatever reason I didn’t notice lxml’s objectify sub-package, but I saw it recently and decided I should check it out. To my mind, the objectify module seems to be even more “Pythonic” than etree is. Let’s take a some time and go over my old XML examples using objectify and see how it’s different! (more…)
Sat 20 Nov 2010
Last time, we looked at one of Python’s built-in XML parsers. In this article, we will look at the fun third-party package, lxml from codespeak. It uses the ElementTree API, among other things. The lxml package has XPath and XSLT support, includes an API for SAX and a C-level API for compatibility with C/Pyrex modules. We’ll just do a few simple things with it though. (more…)
Fri 12 Nov 2010
If you’re a long time reader, you may remember that I started programming Python in 2006. Within a year or so, my employer decided to move away from Microsoft Exchange to the open source Zimbra client. Zimbra is an alright client, but it was missing a good way to alert the user to the fact that they had an appointment coming up, so I had to create a way to query Zimbra for that information and show a dialog. What does all this mumbo jumbo have to do with XML though? Well, I thought that using XML would be a great way to keep track of which appointments had been added, deleted, snoozed or whatever. It turned out that I was wrong, but that’s not the point of this story. (more…)