1 / 11

Parsing XML Code With Python

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?.

russ
Download Presentation

Parsing XML Code With Python

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parsing XML Code With Python Presented By: Dylan Houston Coastal Carolina University

  2. 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?

  3. 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

  4. 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

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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Thanks for your time!

More Related