Learn XML - parsing in python with simple examples

How to handle the XML files and fetch the data out of it?

Following is the useful link for studying with good examples.I have learned from it.

https://docs.python.org/2/library/xml.etree.elementtree.html

  
   
        1
        2008
        141100
        
        
    
    
        4
        2011
        59900
        
    
    
        68
        2011
        13600
        
        
    

This is the example XML content given in that link. We can read or process the XML by two ways. 
1. Reading XML from the file 
2. Giving the whole content as string a variable and then processing it. 

Before we do parsing or reading the XML file, we should know how python xml.etree.ElementTree class is gonna understand the XML file.

1. Tree - The whole XML file is called tree (Well structured and organized) 
2. Root - The data tag is called as root of the XML file 
3. Child - Inside data tag is a Country tag which is called as Child for the root. There may be 'n' of childs possible 
4. GrandChild - Inside the country tag other tags like rank, year, gdppc and neighbor are called other tags or grandchild. Thats what I call by myself.


<data>  data is called as tag (str)
<country name="Liechtenstein">  name is called as attrib (dict) for the country tag
<year>2011</year> 2011 is called as text (str) for the year tag

We will be using the tag, attrib and text fetch those contents out of the well formed XML file.


Comments