Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI/CMPE 4341 Topic: Programming in Python Chapter 9: Python XML Processing Xiang Lian The University of Texas – Pan American Edinburg, TX 78539

Similar presentations


Presentation on theme: "CSCI/CMPE 4341 Topic: Programming in Python Chapter 9: Python XML Processing Xiang Lian The University of Texas – Pan American Edinburg, TX 78539"— Presentation transcript:

1 CSCI/CMPE 4341 Topic: Programming in Python Chapter 9: Python XML Processing Xiang Lian The University of Texas – Pan American Edinburg, TX 78539 lianx@utpa.edu 1

2 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

3 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

4 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

5  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

6 XML Document View XML documents – Any text editor Internet Explorer, Notepad, Visual Studio, etc. 6 Minus sign

7  2002 Prentice Hall. All rights reserved. Outline 7 letter.xml 1 2 3 4 5 6 7 8 Jane Doe 9 Box 12345 10 15 Any Ave. 11 Othertown 12 Otherstate 13 67890 14 555-4321 15 16 17 18 19 John Doe 20 123 Main St. 21 22 Anytown 23 Anystate 24 12345 25 555-1234 26 27 28 29 Dear Sir: 30 Root element letter Child element contact Attribute (name-value pair)Empty elements do not contain character data

8  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. 36 37 38 Please visit our Web site for availability 39 and pricing. 40 41 42 Sincerely 43 44 Ms. Doe 45

9 9 XML Namespaces Provided for unique identification of XML elements Namespace prefixes identify namespace to which an element belongs Topic: Programming in Python

10  2002 Prentice Hall. All rights reserved. Outline 10 namespace.xml 1 2 3 4 5 6 <text:directory xmlns:text = "http://www.deitel.com/ns/python1e" 7 xmlns:image = "http://www.deitel.com/images/ns/120101"> 8 9 10 A book list 11 12 13 14 A funny picture 15 16 17 18 Attribute xmlns creates namespace prefix Namespace prefix bound to an URI Uses prefix text to describe element file

11  2002 Prentice Hall. All rights reserved. Outline 11 defaultnamespace.xml 1 2 3 4 5 6 <directory xmlns = "http://www.deitel.com/ns/python1e" 7 xmlns:image = "http://www.deitel.com/images/ns/120101"> 8 9 10 A book list 11 12 13 14 A funny picture 15 16 17 18 Creates default namespace by binding URI to attribute xmlns without prefixElement without prefix defined in default namespace

12 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 13 Example of Document Object Model (DOM) article title author summary contents lastName firstName date

14 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

15  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. 16.1 Text file names.txt used in Fig. 16.2.

16  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 = [ ( "&", "&" ), ( "<", "<" ), ( ">", ">" ), ( '"', """ ), ( "'", "&apos;" ) ] # 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

17  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 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

19  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

20  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)

21  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 22


Download ppt "CSCI/CMPE 4341 Topic: Programming in Python Chapter 9: Python XML Processing Xiang Lian The University of Texas – Pan American Edinburg, TX 78539"

Similar presentations


Ads by Google