Note: For more information, refer to Working With JSON Data in Python
XML is a markup language which is designed to store data. It is case sensitive. XML offers you to define markup elements and generate customized markup language. The basic unit in the XML is known as an element. The XML language has no predefined tags. It simplifies data sharing, data transport, platform changes, data availability Extension of an XML file is .xml
Note: For more information, refer to XML | Basics
Both JSON and XML file format are used for transferring data between client and server.
However, they both serve the same purpose though differ in their on way.
Comparison between JSON and XML
|JSON object has a type||XML data is typeless|
|JSON types: string, number, array, Boolean||All XML data should be string|
|Data is readily accessible as JSON objects||XML data needs to be parsed|
|JSON is supported by most browsers||Cross-browser XML parsing can be tricky|
|JSON has no display capabilities||XML offers the capability to display data because it is a markup language|
|JSON supports only text and number data type.||XML support various data types such as number, text, images, charts, graphs, etc. It also provides options for transferring the structure or format of the data with actual data.|
|Retrieving value is easy||Retrieving value is difficult|
|Supported by many Ajax toolkit||Not fully supported by Ajax toolkit|
|Native support for object||The object has to be express by conventions – mostly missed use of attributes and elements.|
|It supports only UTF-8 encoding.||It supports various encoding|
|It doesn’t support comments.||It supports comments.|
|JSON files are easy to read as compared to XML.||XML documents are relatively more difficult to read and interpret.|
|It does not provide any support for namespaces||It supports namespaces.|
|It is less secured.||It is more secure than JSON.|
Handling JSON in Python 3
To handle the JSON file format, Python provides a module named
STEP 1: import the json module
import json as JS
STEP 2: import xml.etree.ElementTree module
import xml.etree.ElementTree as ET
STEP 3: Read the json file
here, “data” is the variable in which we have loaded our JSON data.
with open("quiz.json", "r") as json_file: data = JS.load(json_file);
STEP 4: Build the root element
Every xml file must have exactly one root element
root = ET.Element("quiz")
STEP 5: Build the subelements of the root
SubElement takes two parameters:
- root- It is the name of the variable where root element is stored.
- subelement_name: It is the name of subelement.Example:
Maths = ET.SubElement(root, "maths")
STEP 6: Build the tree of xml document
tree = ET.ElementTree(root)
STEP 7: Write the xml to quiz.xml file
Note : XML elements does not support integer values so we need to convert them to string.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course