Prognoz Technologies Pvt Ltd Empowering through Skill Development

  • Slides: 19
Download presentation
Prognoz Technologies Pvt. Ltd Empowering through Skill Development XML Programming with Python Version 1.

Prognoz Technologies Pvt. Ltd Empowering through Skill Development XML Programming with Python Version 1. 0

Objectives Topic Covered: § § § Python using XML JSON Introduction & syntax Read

Objectives Topic Covered: § § § Python using XML JSON Introduction & syntax Read & Write JSON File © Prognoz Technologies Pvt. Ltd

Python Using Xml § What is XML? – XML stands for e. Xtensible Markup

Python Using Xml § What is XML? – XML stands for e. Xtensible Markup Language. It was designed to store and transport small to medium amounts of data and is widely used for sharing structured information. § XML is a tag-based language for defining structured information, commonly used to define documents and data shipped over the Web and other applications. § To get information from XML, developers need to parse the XML § Python enables you to parse and modify XML document. In order to parse XML document you need to have the entire XML document in memory. © Prognoz Technologies Pvt. Ltd

Continue…. § There are different ways to parse a XML file in Python 1.

Continue…. § There are different ways to parse a XML file in Python 1. Parse XML using re Pattern Module. 2. Parse XML using DOM parser. 3. Parse XML using SAX parser. 4. Parse XML using Element. Tree from etree package. © Prognoz Technologies Pvt. Ltd

1. Parse XML using re Pattern Module. § Python has inbuilt support of pattern

1. Parse XML using re Pattern Module. § Python has inbuilt support of pattern matching which is available under re Pattern Module. One can parse xml file using pattern matching as follow. Example: Output: © Prognoz Technologies Pvt. Ltd

2. Parse XML using DOM parser § DOM parser is most standard and widely

2. Parse XML using DOM parser § DOM parser is most standard and widely used parser for XML. it parses XML text into a tree of objects and provides an interface for navigating the tree to extract tag attributes and values. Below example demonstrates parsing of XML using DOM parser. Example: Output: © Prognoz Technologies Pvt. Ltd

3. Parse XML using SAX parser. § Python has standard library for SAX parser.

3. Parse XML using SAX parser. § Python has standard library for SAX parser. § In SAX model, a class’s methods receive callbacks as a parse progresses and use state information to keep track of where they are in the document and collect its data. § Below example demonstrates parsing of XML using SAX parser. © Prognoz Technologies Pvt. Ltd

Example… Output: © Prognoz Technologies Pvt. Ltd

Example… Output: © Prognoz Technologies Pvt. Ltd

4. Parse XML using Element. Tree from etree package. § This method to parse

4. Parse XML using Element. Tree from etree package. § This method to parse XML has similar effect as DOM parse. However, lesser lines of code needs to perform similar task. the Element. Tree system available in the etree package of the standard library in Python. Below example demonstrates parsing of XML using Element. Tree. Example: © Prognoz Technologies Pvt. Ltd Output:

JSON Introduction § JSON stands for Java. Script Object Notation and was proposed by

JSON Introduction § JSON stands for Java. Script Object Notation and was proposed by Douglas Crockford. § JSON is an easy to understand data exchange format which is independent of programming languages. § Why JSON? – JSON allows websites to load data quickly and asynchronously in the background without much delay. – JSON doesn’t have end tags like XML, is shorter than XML and lightweight. – JSON is preferred way of exchanging data in AJAX requests and in REST architecture. – JSON data is offered by API’s hosted by Google, Facebook, Microsoft, Sales. Force, Twitter, Linked. In and others. © Prognoz Technologies Pvt. Ltd

JSON Syntax § JSON is made up of string: value pairs. § An object

JSON Syntax § JSON is made up of string: value pairs. § An object represents these string: value pairs. § An object may contain a number of individual string: value pairs or a string may contain multiple string: value pairs (nested string: value pairs) or an array of string: value pairs (array of objects) or all of these. § An object should be enclosed within parenthesis { } and an array of objects should be enclosed within square brackets [ ]. § Each string is followed by a : (colon) and each of these string: value pairs should be separated by a comma. © Prognoz Technologies Pvt. Ltd

JSON Syntax § An object with Individual string: value pairs – This JSON object

JSON Syntax § An object with Individual string: value pairs – This JSON object has individual string: value pairs, separated by comma and enclosed within parenthesis { }. § An object with string containing multiple string: value pairs (nested string: value pairs) – This JSON object is already enclosed within parenthesis { } and also the string “Contact” has few other nested string: value pairs. © Prognoz Technologies Pvt. Ltd

Continue…. § An object with array of string: value pairs (array of JSON objects)

Continue…. § An object with array of string: value pairs (array of JSON objects) – This JSON object is enclosed within parenthesis { }. The string “Address” is followed by an array of JSON objects separated by comma and enclosed within brackets [ ]. © Prognoz Technologies Pvt. Ltd

Continue…. § Below figure depicts how JSON verbs compare with Python identifiers. © Prognoz

Continue…. § Below figure depicts how JSON verbs compare with Python identifiers. © Prognoz Technologies Pvt. Ltd

Read JSON with Python Example: © Prognoz Technologies Pvt. Ltd

Read JSON with Python Example: © Prognoz Technologies Pvt. Ltd

Output: © Prognoz Technologies Pvt. Ltd

Output: © Prognoz Technologies Pvt. Ltd

Write JSON with Python We create a Python dictionary data and is converted to

Write JSON with Python We create a Python dictionary data and is converted to JSON objects by using the method dumps from json library. © Prognoz Technologies Pvt. Ltd

Continue… Output: © Prognoz Technologies Pvt. Ltd

Continue… Output: © Prognoz Technologies Pvt. Ltd

Summary!!! © Prognoz Technologies Pvt. Ltd

Summary!!! © Prognoz Technologies Pvt. Ltd