CSCI/CMPE 4341 Topic: Programming in Python Chapter 9: Python XML Processing Xiang Lian The University of Texas – Pan American Edinburg, TX
Objectives In this chapter, you will: – Understand XML – Become familiar with the types of markup languages created with XML – Learn to create XML markup programmatically – Use the Document Object Model (DOM) to manipulate XML documents – Explore ElementTree package to retrieve data from XML documents 2
Introduction XML developed by World Wide Consortium’s (W3C’s) XML Working Group (1996) XML portable, widely supported, open technology for describing data XML quickly becoming standard for data exchange between applications 3
XML Documents XML documents end with.xml extension XML marks up data using tags, which are names enclosed in angle brackets – elements – Elements: individual units of markup (i.e., everything included between a start tag and its corresponding end tag) – Nested elements form hierarchies – Root element contains all other document elements 4
2002 Prentice Hall. All rights reserved. Outline 5 article.xml Simple XML December 21, 2001 John Doe XML is pretty easy. In this chapter, we present a wide variety of examples that use XML. Optional XML declaration includes version information parameter XML comments delimited by Root element contains all other document elements End tag has format
XML Document View XML documents – Any text editor Internet Explorer, Notepad, Visual Studio, etc. 6 Minus sign
2002 Prentice Hall. All rights reserved. Outline 7 letter.xml Jane Doe 9 Box Any Ave. 11 Othertown 12 Otherstate John Doe Main St Anytown 23 Anystate Dear Sir: 30 Root element letter Child element contact Attribute (name-value pair)Empty elements do not contain character data
2002 Prentice Hall. All rights reserved. Outline 8 letter.xml 31 It is our privilege to inform you about our new 32 database managed with XML. This 33 new system allows you to reduce the load on 34 your inventory list server by having the client machine 35 perform the work of sorting and filtering the data Please visit our Web site for availability 39 and pricing Sincerely Ms. Doe 45
9 XML Namespaces Provided for unique identification of XML elements Namespace prefixes identify namespace to which an element belongs Topic: Programming in Python
2002 Prentice Hall. All rights reserved. Outline 10 namespace.xml <text:directory xmlns:text = " 7 xmlns:image = " A book list A funny picture Attribute xmlns creates namespace prefix Namespace prefix bound to an URI Uses prefix text to describe element file
2002 Prentice Hall. All rights reserved. Outline 11 defaultnamespace.xml <directory xmlns = " 7 xmlns:image = " A book list A funny picture Creates default namespace by binding URI to attribute xmlns without prefixElement without prefix defined in default namespace
12 Document Object Model (DOM) DOM parser retrieves data from XML document Hierarchical tree structure called a DOM tree – Each component of an XML document represented as a tree node – Parent nodes contain child nodes – Sibling nodes have same parent – Single root (or document) node contains all other document nodes
13 Example of Document Object Model (DOM) article title author summary contents lastName firstName date
Processing XML in Python Python packages for XML support – 4DOM and xml.sax – Generating XML dynamically similar to generating HTML – Python scripts can use print statements or XSLT to output XML 14
2002 Prentice Hall. All rights reserved. Outline 15 names.txt O'Black, John Green, Sue Red, Bob Blue, Mary White, Mike Brown, Jane Gray, Bill Fig Text file names.txt used in Fig
2002 Prentice Hall. All rights reserved. Outline 16 fig16_02.py #!c:\Python\python.exe # Fig. 16.2: fig16_02.py # Marking up a text file's data as XML. import sys # write XML declaration and processing instruction print (""" """) # open data file try: file = open( "names.txt", "r" ) except IOError: sys.exit( "Error opening file" ) print (" ") # write root element # list of tuples: ( special character, entity reference ) replaceList = [ ( "&", "&" ), ( "<", "<" ), ( ">", ">" ), ( '"', """ ), ( "'", "'" ) ] # replace special characters with entity references for currentLine in file.readlines(): for oldValue, newValue in replaceList: currentLine = currentLine.replace( oldValue, newValue ) Print XML declarationOpen text file if it existsPrint root elementList of special characters and their entity referencesReplace special characters with entity references
2002 Prentice Hall. All rights reserved. Outline 17 fig16_02.py # extract lastname and firstname last, first = currentLine.split( ", " ) first = first.strip() # remove carriage return # write contact element print (""" %s """ % ( last, first )) file.close() print (" ") Extract first and last nameRemove carriage returnPrint contact elementPrint root’s closing tag
18 XML Processing Packages Third-party package 4DOM, included with package PyXML, complies with W3C’s DOM Recommendation xml.sax, included with Python, contains classes and functions for SAX-based parsing 4XSLT, located in package 4Suite, contains an XSLT processor for transforming XML documents into other text-based formats import xml.etree.ElementTree
2002 Prentice Hall. All rights reserved. Outline 19 article2.xml Simple XML December 19, 2001 Jane Doe XML is easy. Once you have mastered XHTML, XML is learned easily. Remember that XML is not for displaying information but for managing information. XML document used by fig16_04.py
2002 Prentice Hall. All rights reserved. Outline 20 fig16_04.py # Fig. 16.4: fig16_04.py # Using 4DOM to traverse an XML Document. import sys import xml.etree.ElementTree as etree # open XML file try: tree = etree.parse("article2.xml") except IOError: sys.exit( "Error opening file" ) # get root element rootElement = tree.getroot() print ("Here is the root element of the document: %s" % \ rootElement.tag) # traverse all child nodes of root element print ("The following are its child elements:" ) for node in rootElement: print (node)
2002 Prentice Hall. All rights reserved. Outline fig16_04.py # get first child node of root element child = rootElement[0] print ("\nThe first child of root element is:", child.tag) print ("whose next sibling is:" ) # get next sibling of first child sibling = rootElement[1] print (sibling.tag) print ('Value of "%s" is:' % sibling.tag, end="") print (sibling.text)
22