260 likes | 1.01k Views
Parsing XML Code With Python. Presented By: Dylan Houston Coastal Carolina University. View an XML file without manually browsing through it Utilize XML Programming Python- Minidom Parser 7 Fundamental Functions of Python XML Programming. What if I don’t want to read through the XML?.
E N D
Parsing XML Code With Python Presented By: Dylan Houston Coastal Carolina University
View an XML file without manually browsing through it • Utilize XML Programming • Python- Minidom Parser • 7 Fundamental Functions of Python XML Programming What if I don’t want to readthrough the XML?
from xml.dom import minidom • This is the minidom library imported. Placed at the top of the program beside defined constants and other imported libraries. • dom = minidom.parse("document.xml") • Declares a variable dom using the minidom library’s parse function. Initiate DOM
x = dom.getElementsByTagName(‘tag’) • This method performs the task of obtaining the data in the XML document. • Takes one parameter, which defines the tag from which data is to be retrieved. Fundamental 1:getElementsByTagName
NAMESPACE= ‘http://namespaceurl/’ • x = dom.getElementsByTagNameNS(NAMESPACE, ‘tag’) • Unlike the previous method, this method is able to handle namespaces. • The first argument is the namespace URL (defined above). • The second is the tag name within the namespace. Fundamental 2:getElementsByTagNameNS
x= dom.getAttribute(‘att’) • Searches XML • Returns a string type value from the attribute specified • If no value exists, empty string is returned. • Parameter taken is the attribute desired. Fundamental 3:getAttribute
x= dom.getAttributeNS(NS, ‘att’) • Retrieves an element with an attribute within a namespace. • Receives a value from an attribute “att” within namespace “NS” and assigns the value to x. Fundamental 4:getAttributeNS
x=dom.getElementsByTagNameNS(NAMESPACE, tagName)[0].firstChild.data)” • Mainly syntactical, but does serve a purpose. (gets the 0th element) • It references the first “child” element within the tag and retrieves that value and assigns it to x. Fundamental 5:Utilizing “firstchild.data” to retrieve elements
Lists are not limited to storing data to only one data type. • In XML linkbase documents, parallel lists are handy because they can store “raw” hard-to-read tags, as well as their human readable counterparts. • Also good for storing elements with corresponding number values Fundamental 6:Parallel Lists
Using these 7 fundamentals and some basic programming knowledge, XML Programming is possible. • Python example 1: Parsing and storing elements in XML Linkbase document • Python example 2: Parsing XBRL financial statement with Object Oriented Programming to view human readable statement Fundamental 7:Putting it All Together