DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Use Case: XML Parsing With Python

Use Case: XML Parsing With Python

Mike Driscoll user avatar by
Mike Driscoll
·
Jul. 18, 12 · Interview
Like (0)
Save
Tweet
Share
11.84K Views

Join the DZone community and get the full member experience.

Join For Free

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:

<?xml version="1.0"?>
<invoice>
    <order_number>456789</order_number>
	<customer_id>789654</customer_id>
	<address1>John Doe</address1>
	<address2>123 Dickens Road</address2>
	<address3>Johnston, IA 55555</address3>
	<address4/>
	<order_items>
		<item>
			<id>11123</id>
			<name>Expo Dry Erase Pen</name>
			<price>1.99</price>
			<quantity>5</quantity>
		</item>
		<item>
			<id>22245</id>
			<name>Cisco IP Phone 7942</name>
			<price>300</price>
			<quantity>1</quantity>
		</item>
		<item>
			<id>33378</id>
			<name>Waste Basket</name>
			<price>9.99</price>
			<quantity>1</quantity>
		</item>
	</order_items>
</invoice>

Save the code above as order.xml. Now I just need to write a parser and PDF generator script in Python. You can use Python builtin XML parsing libraries which include SAX, minidom or ElementTree or you can go out and download one of the many external packages for XML parsing. My favorite is lxml which includes a version of ElementTree as well as a really nice piece of code that they call "objectify". This latter piece will basically take XML and turn it into a dot notation Python object. I'll be using it to do our parsing because it is so straight-forward, easy to implement and understand. As stated earlier, I'll be using Reportlab to do the PDF creation piece.  Here's a simple script that will do everything we need:

 

from decimal import Decimal
from lxml import etree, objectify

from reportlab.lib import colors
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch, mm
from reportlab.pdfgen import canvas
from reportlab.platypus import Paragraph, Table, TableStyle

########################################################################
class PDFOrder(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, xml_file, pdf_file):
        """Constructor"""
        self.xml_file = xml_file
        self.pdf_file = pdf_file
        
        self.xml_obj = self.getXMLObject()
        
    #----------------------------------------------------------------------
    def coord(self, x, y, unit=1):
        """
        # http://stackoverflow.com/questions/4726011/wrap-text-in-a-table-reportlab
        Helper class to help position flowables in Canvas objects
        """
        x, y = x * unit, self.height -  y * unit
        return x, y  
        
    #----------------------------------------------------------------------
    def createPDF(self):
        """
        Create a PDF based on the XML data
        """
        self.canvas = canvas.Canvas(self.pdf_file, pagesize=letter)
        width, self.height = letter
        styles = getSampleStyleSheet()
        xml = self.xml_obj
        
        address = """ <font size="9">
        SHIP TO:<br/>
        <br/>
        %s<br/>
        %s<br/>
        %s<br/>
        %s<br/>
        </font>
        """ % (xml.address1, xml.address2, xml.address3, xml.address4)
        p = Paragraph(address, styles["Normal"])
        p.wrapOn(self.canvas, width, self.height)
        p.drawOn(self.canvas, *self.coord(18, 40, mm))
        
        order_number = '<font size="14"><b>Order #%s </b></font>' % xml.order_number
        p = Paragraph(order_number, styles["Normal"])
        p.wrapOn(self.canvas, width, self.height)
        p.drawOn(self.canvas, *self.coord(18, 50, mm))
        
        data = []
        data.append(["Item ID", "Name", "Price", "Quantity", "Total"])
        grand_total = 0
        for item in xml.order_items.iterchildren():
            row = []
            row.append(item.id)
            row.append(item.name)
            row.append(item.price)
            row.append(item.quantity)
            total = Decimal(str(item.price)) * Decimal(str(item.quantity))
            row.append(str(total))
            grand_total += total
            data.append(row)
        data.append(["", "", "", "Grand Total:", grand_total])
        t = Table(data, 1.5 * inch)
        t.setStyle(TableStyle([
            ('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
            ('BOX', (0,0), (-1,-1), 0.25, colors.black)
        ]))
        t.wrapOn(self.canvas, width, self.height)
        t.drawOn(self.canvas, *self.coord(18, 85, mm))
        
        txt = "Thank you for your business!"
        p = Paragraph(txt, styles["Normal"])
        p.wrapOn(self.canvas, width, self.height)
        p.drawOn(self.canvas, *self.coord(18, 95, mm))
        
    #----------------------------------------------------------------------
    def getXMLObject(self):
        """
        Open the XML document and return an lxml XML document
        """
        with open(self.xml_file) as f:
            xml = f.read()
        return objectify.fromstring(xml)
    
    #----------------------------------------------------------------------
    def savePDF(self):
        """
        Save the PDF to disk
        """
        self.canvas.save()
    
#----------------------------------------------------------------------
if __name__ == "__main__":
    xml = "order.xml"
    pdf = "letter.pdf"
    doc = PDFOrder(xml, pdf)
    doc.createPDF()
    doc.savePDF()
	

 Let's take a couple minutes to go over this code. First off is a bunch fo imports. This just sets up our environment with the needed compents from Reportlab and lxml. I also import the decimal module as I will be adding amounts and it is much more accurate for float mathematics than just using normal Python math. Next we create our PDFOrder class which accepts two arguments: an xml file and a pdf file path. In our initialization method, we create a couple class properties, read the XML file and return an XML object. The coord method is for positioning Reportlab flowables, which are dynamic objects with the ability to split across pages and accept various styles. The createPDF method is the meat of the program. The canvas object is used to create our PDF and "draw" on it. I set it up to be letter sized and I also grab a default stylesheet.
Next I create a shipping address and position it near the top of the page, 18mm from the left and 40mm from the top. After that, I create and place the Order Number. Finally, I iterate over the items in the order and place them in a nested list, which is then placed in Reportlab's Table flowable. Finally, I position the table and pass it some styles to give it a border and an inner grid. Lastly, we save the file to disk.  The document is created and I've now got a nice prototype to show my colleagues. At this point, all I need to do is tweak the look and feel of the document by passing in different styles for the text (i.e. bold, italic, font size) or changing the layout a bit. This is usually up to management or the client, so you'll have to wait and see what they want.  Now you know how to parse an XML document in Python and create a PDF from the parsed data.

XML Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • AIOps Being Powered by Robotic Data Automation
  • How To Validate Three Common Document Types in Python
  • Top 5 Node.js REST API Frameworks
  • Best Practices for Writing Clean and Maintainable Code

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: