Write a note on XML. Design python program to retrieve a node present in the XML tree. (08 Marks)
This question was asked in Python Application Programming 15CS664 Jan 2019 question paper for 8 Marks.
eXtensible Markup Language – XML
XML document looks very similar to HTML (HyperText Markup Language), but XML is more structured than HTML document. XML is used to transfer data in standard form from one machine to another machine.
Here is a sample example of XML document:
<person> <name>Rahul</name> <phone type="mobile"> +91 9989898989 </phone> <email hide="yes"/> </person>
Unlike HTML tags, Tags in XML identify the type of data and are used to store and organize the different type of data, rather than specifying how to display it, which are used to display the data.
XML document looks like a tree structure where there is a top tag person acts as the root of the tree and other tags such as phone are drawn as children of their parent nodes.
Here is a simple example application program in python that parses some XML document and extracts the value of data elements (tag) from the XML. This program Python program to retrieve a node present in the XML tree.
import xml.etree.ElementTree as ET data = ''' <person> <name>Rahul</name> <phone type="mobile">+91 7151237177</phone> <email hide="yes"/> </person>''' tree = ET.fromstring(data) print('Name:', tree.find('name').text) print('Mobile No:', tree.find('phone').text)
xml.etree.ElementTree is used to parse the XML document. It has a function called fromstring, which takes XML document as input and converts into the string representation of the XML into a “tree” of XML nodes.
When the XML is in a tree, we have a series of methods we can call to extract portions of data from the XML. find function is used to extract the value of a tag. In the above example tree.find(‘phone’).text returns the phone number.
Clik here to read Solution to Python Application Programming Question Paper Jan 2019 15CS664
If you like the post share with your friends. For regular updates on VTU CBCS Notes, Question Papers, interview study material, python programs, etc, do like our facebook page.