๐ด ๐๐๐๐ซ๐ง ๐๐ซ๐๐ง๐๐ข๐ง๐ ๐๐๐๐ก๐ง๐จ๐ฅ๐จ๐ ๐ข๐๐ฌ ๐ ๐จ๐ซ ๐ ๐ซ๐๐! ๐๐ฎ๐๐ฌ๐๐ซ๐ข๐๐ ๐ญ๐จ ๐๐๐ฎ๐ซ๐๐ค๐ ๐๐จ๐ฎ๐๐ฎ๐๐ ๐๐ก๐๐ง๐ง๐๐ฅ: https://2ly.link/1w69F This Edureka video on 'Arrays in Python' will help you establish a strong hold on all the fundamentals in python programming language. ๐ข๐ข ๐๐จ๐ฉ ๐๐ ๐๐ซ๐๐ง๐๐ข๐ง๐ ๐๐๐๐ก๐ง๐จ๐ฅ๐จ๐ ๐ข๐๐ฌ ๐ญ๐จ ๐๐๐๐ซ๐ง ๐ข๐ง ๐๐๐๐ ๐๐๐ซ๐ข๐๐ฌ ๐ข๐ข โฉ NEW Top 10 Technologies To Learn In 2024 - https://www.youtube.com/watch?v=vaLXPv0ewHU ๐ด Subscribe to our channel to get video updates. Hit the subscribe button above: https://goo.gl/6ohpTV ๐ด ๐๐๐ฎ๐ซ๐๐ค๐ ๐๐ง๐ฅ๐ข๐ง๐ ๐๐ซ๐๐ข๐ง๐ข๐ง๐ ๐๐ง๐ ๐๐๐ซ๐ญ๐ข๐๐ข๐๐๐ญ๐ข๐จ๐ง๐ฌ ๐ต DevOps Online Training: http://bit.ly/3VkBRUT ๐ AWS Online Training: http://bit.ly/3ADYwDY ๐ต React Online Training: http://bit.ly/3Vc4yDw ๐ Tableau Online Training: http://bit.ly/3guTe6J ๐ต Power BI Online Training: http://bit.ly/3VntjMY ๐ Selenium Online Training: http://bit.ly/3EVDtis ๐ต PMP Online Training: http://bit.ly/3XugO44 ๐ Salesforce Online Training: http://bit.ly/3OsAXDH ๐ต Cybersecurity Online Training: http://bit.ly/3tXgw8t ๐ Java Online Training: http://bit.ly/3tRxghg ๐ต Big Data Online Training: http://bit.ly/3EvUqP5 ๐ RPA Online Training: http://bit.ly/3GFHKYB ๐ต Python Online Training: http://bit.ly/3Oubt8M ๐ต GCP Online Training: http://bit.ly/3VkCzS3 ๐ Microservices Online Training: http://bit.ly/3gxYqqv ๐ต Data Science Online Training: http://bit.ly/3V3nLrc ๐ CEHv12 Online Training: http://bit.ly/3Vhq8Hj ๐ต Angular Online Training: http://bit.ly/3EYcCTe ๐ด ๐๐๐ฎ๐ซ๐๐ค๐ ๐๐จ๐ฅ๐-๐๐๐ฌ๐๐ ๐๐จ๐ฎ๐ซ๐ฌ๐๐ฌ ๐ต DevOps Engineer Masters Program: http://bit.ly/3Oud9PC ๐ Cloud Architect Masters Program: http://bit.ly/3OvueZy ๐ต Data Scientist Masters Program: http://bit.ly/3tUAOiT ๐ Big Data Architect Masters Program: http://bit.ly/3tTWT0V ๐ต Machine Learning Engineer Masters Program: http://bit.ly/3AEq4c4 ๐ Business Intelligence Mas
Full Transcript
hey guys welcome back to a new live session from Eda my name is vaja and in this session you'll be learning how to parse XML using python coming back towards this session we shall first begin with a small introduction to XML and parsing then we shall take a look at the python parsing modules namely Elementary module and the minidom module and how you can make use of these to parse modify or look for data of your interest so I hope everyone's ready to start with this so without any further dues let's get started by understanding what exactly is XML XML stands for extensible markup language it is similar to HTML in its appearance but XML is used for data presentation while HTML is used to define what data is being used XML is exclusively designed to send and receive data back and forth between clients and servers now just to show you all how data looks in the XML format let me jump on to pycharm and show you the sample file that I'll be using in this session today so as you all can see on the screen it basically begins with a less than symbol a question mark symbol followed by xml's version and the type of encoding that's being used it has root Elements which can have n number of child elements present in it so here my root element is metadata and the child elements of this is food item Etc you can Define your own tags but remember to close each tag after you define it so as you all can see on the screen all the tags that have opened have a closing tag as well and the root tag will close at the end of this file so I hope everyone's clear with this now let's get back to our presentation and see what exactly is parsing parsing basically means to read information and split it into into pieces by identifying the paths of that particular XML document now let's move on and take a look at python XML parsing modules in this session I'll be making use of the elementary and the minidom module to parse XML using python the elementary module helps us format XML data in a tree structure which is the most natural representation of hierarchical data the element data type allows storage of hierarchical data structures in memory the minidom module is basically used by people who are proficient with dom or the document object module Dom applications often start by passing XML into Dom okay now let's move on to take a deeper look at these modules beginning with the elementary module as mentioned earlier this module helps format XML data in a tree structure Elementary is a class that wraps the element structure and allows conversion to and from XML and it it has the following properties so basically each element that's present in the elementary module will consist of a tag that describes the type of data being stored the attributes which are basically stored as python dictionaries a text string consisting of the information that needs to be displayed a tail string which is optional and child Elements which consists of other specific data in today's session we'll be seeing how to use this module for parsing finding elements and to modify that is adding or deleting elements from an XML document so first let's take a look at how to pass XML using the elementary module there are two ways to pass a file using this module the first is by using the pass function and the second is to use the from string function the pass function passes an XML document which is supplied to it as a file now let's jump on to pycharm to see how exactly this works I'll create a new file I'll just name it as example okay the first thing that you'll have to do is import this module using import xml. e3. Elementary and I'll give it an alas name as ET after this I'll just take a variable say my tree and I'll use ET do pars and I'll pass this sample XML file as a parameter to this okay now let me just execute this as you all can see on the screen the process is finished without any errors which means my file has been passed now to get the root element of this file what I'm going to do is take another variable say my root and then I'll use the get root method to fetch the root element of this file after this I'll just print my root and now let me hit run so as you all can see over here when I Ed the get root method it has returned the root element of my file which is metadata so this is how you can pass an XML file by passing it as a parameter to the pass function now in case you want to pass your XML data as a string in this case you can make use of the from string method to do this I'll just create a new python file and I'll just name it as example two and I'll just copy this import statement and after this I'll just Define some variable say data into which I'll store the XML string that I want to pass using the from string method I'll specify this XML string within triple codes let me just copy one part of this XML file and put it within this triple codes I'll have to close the root tag which is metadata and then I'll say my rot is equal to et. from string and I'll pass data as the parameter to this sorry guys I've made a spelling mistake over here I have to use ET do from string and then I'll print my root do tag as you all can see my string has been passed and the tag of the root element has been returned which is metadata so I hope everyone's clear till here okay so in case you have any doubts please do let me know in the chat box and my team is here to help you so now let's move on and see how you can actually find elements of your interest using the elementary module guys please make a note that last time I just used print my root and this time I used print my root. tag and that is why it has returned the root tag for me in case I would not use this attribute it would have have returned the same output as the previous output now let's move on and see how you can actually find the elements of your interest using the elementary module you can find various elements and sub elements using attributes such as tag attrib text Etc so like I've showed you earlier in case you want to print the tag of the root element you just have to say my root. tag and run this so as you all can see the tag element has been returned now in case I want to print the tag of the first child of root element all I have to do is copy this and specify the subscript as zero which will return the first child of my root element now let me hit run as you can see over here the first child of my root element is food let's get back to our sample. XML and see where food is exactly present as you all can see the first child of this root element is food and over here when I use the subscript as zero it has returned the first child of the root element which is food so I hope everyone's clear with this okay now in case you want to check what is the attribute that's present for the food tag so for that I'll just replace this tack attribute by attrib and then I'll hit run so like I've told you earlier attributes are present as dictionaries the food tag has no extra attributes and therefore my output is an empty dictionary now in case I want to print out all the tags and attributes that are present within the food tag I can just Loop through it using the for Loop and say for X in my root of zero I'll print out x. tag and x. attrib this is X and then I hit run so as you all can see over here all the tags and attributes that are present within the food tag have been returned to bring out the text that's present Within These tags you can make use of the text attribute so to print out the text present in all the tags I'll just use for X in my root of zero I'll just say print x. text and then I'll hit run so as you can see all the text that's present within each tag has been returned as my output so now let me just remove this part so as to make my output clear now in case I want to find all the items along with its prices I'll use the find all method so for for X in my root doind all and I'll specify the keyword as food since I want to Loop through all the food tags I'll specify food as a parameter to this and then I'll just take another variable say item and I'll use x. find so this time I want to print out the item name which in the first case is idly so I want to find the items in this in the Four tag I want to retrieve the item so I'll just use find the text that's present in item and since I want to print the price as well I'll take another variable as price and then I'll say x. find price and I'll print the text in this finally I'll just print both the variables that's it and price now I'll hit run so as you all can see on the screen all the items along with the prices has been returned as my output so I hope everyone's clear till here okay in case you have any doubts please do let me know in the chat box and my team is here to help you now let's get back to our presentation and see how you can actually modify that is add or delete elements of your choice using the elementary module first let me take an example of how you can actually add something to your XML file that's already present so as you all can see on the screen to do this I'll be making use of the set method now let's get back to pyam and try to modify the file that's already present which is sample. XML now in case I want to add on some more description to this description tag all I have to do is use the for Loop and then I'll say for X in my root do it and then I want to iterate over the description tag I'll just take a variable that will store the new description and to this since the data is in string format I'll have to pass a string to this and to the text of this description I want to add a new part say description has been added and then into X of text I'll store a and then I'll use the set method to set another attribute with the name updated and the value as yes I'll just write this altered output to a new file since I don't want to manipulate the original file so I'll use my tree. write and then I'll specify a new name to this file say new. XML so as you all can see over here a new file has been created okay so as you all can see on the screen when I open this file a new description saying description has been added to all the description tags that are present in this file so I hope everyone's clear with this let me just show you the complete file I'll just close this and as you all can see all the description tags have this updated text saying description has been added and a new attribute saying updated and the value being yes has been added to all the description tags present in this file with the help of the right function I was able to create a new XML file and write the updated output to the same you can modify the original file as well however guys you can manipulate the original file as well by specifying the name of the original file itself as the parameter to this in case you want to add a new element you can make use of the sub element method to do this all you have to do is say ET do subelement which is the name of the function and then you'll have to specify where you want to add this tag so I want to add this tag within the food tag so so I'll say my root of zero and I'll name this tag as a speciality tag then I'll iterate over all the food tags that are present and then I'll use the for Loop and for X in my root for X in my root. iter I want to iterate over the speciality tag which I've newly created to this I'll add some new description say B and I'll store it within a variable say B and I'll just pass some string to this say South Indian special you can give any description of your choice guys and then just like I've done before I'll say x. text equals the string of B after this I'll write the output to a new file so for this I'll use my tree. write I've made a spelling mistake sorry guys my tree. write and I'll say new 2.xml and I'll hit run so as you all can see over here a new file has been created and when I open this file a new tag which is the speciality tag has been added to the first food tag that's present in my XML file okay so I hope everyone's clear till here so now let's move on and see how you can delete attributes or sub elements using the elementary module let me just clear this now in case I want to remove some attribute that's present in my file so as you all can see I have a attribute to this item tag now if I want to remove this attribute all I have to do is make use of the pop function to do this I'll say my root and I'll specify the subscript as zero which will be the first child of the root element and then I'll specify another subscript which will be the first child of the food tag so as we all know metadata is the root tag and the first child of this is food tag and within the food tag the first tag that's present is the item tag okay now from this I want to remove the attribute so I'll say attrib do pop and I'll pass the name of it which is name and after this I'll just print the output to a new file so for that I'll use my tree. write and I'll specify the name as new three. XML and I hit run now when I open this file you all can see that the name attribute that was present within the item tag has been removed if you want to remove a complete tag you can make use of the remove method by just specifying which tag you want to remove so for this I'll say my root of zero which means within the food tag I want to remove the first tag so for that I'll say my root and subscript of zero and zero then I'll just write it I'll just write the output so as you all can see the first tag which was item that was present within the food tag has been removed there's another method which is the clear method which can also be used to remove some tag of your choice say for example I want to remove the first food tag that's present in my XML file to do this I'll just say my root of zero my root of0 do clear and then I'll just write the output let me just open the original file and show you that the first tag which was containing description about idly has been removed from the output after using the clear method so in my original file I had idly as the first item and in the new file which is the output has the second item which is paper Dosa as the first item okay so I hope everyone's clear to this now let's get back to our presentation and see how to use the minidom module for parsing finding elements and to find the length of the elements present in our XML file now just like the elementary module the minidom module also has two ways to pass your data the first is to make use of the pass function by supplying the XML file to be passed as a parameter to it this also can be done in two ways let me just get back to pycharm to show this in detail I'll just open a new file since this is a new library I'll say new python file and I'll say example three so the first thing that you have to do to make use of this module is to import it so I'll use from xml.dom import Mini after this I'll just take a variable say myri again and then I'll use minidom dop pars within this just like before I'll have to specify the name of the file so it's sample. XML and then I'll hit run as you all can see the process has finished without any errors however you can make use of this pass function to pass an open file as well to do that I'll just comment this line I'll just take another variable say data and within this I'll store the contents of a file which I want to open so this will be my sample. XML again and then then I'll take another variable say a and then I'll use minidom do pars and I'll pass data as a parameter to this so as you all can see just like before the process is executed without any errors okay so now let me just remove this there's another function known as the par string function which can be used to pass XML when it's applied as a string to it just to show you how to make use of this function I'll take another variable say data again and then I'll use the par string function which is present in the minidom module and to this I'll just pass a string which I already have as a parameter so I'll just copy this and I'll pass this as a parameter and now I'll hit run as you all can see I've used the pass string function to pass an XML string since I won't be using this function in future I'll just remove it okay now let's get back to our presentation and see how you can actually find elements using the minidom module to find various elements that are present in the XML file you can use the get element by name method or value data attributes Etc now I'll jump onto my pycharm and show you all how you can use the get element by tag name method so let's see how you can actually get the tag name using this method I'll take a new variable say tag name enter this I'll specify my tree my tree. getet get element by tag name just make sure you don't make any spelling mistakes within this I'll just PR specify that I want to retrieve the item tag and I'll specify the subscript as zero which means I want to retrieve the first item tag and then I'll just print the tag name okay I've just made spelling mistake over here so it's get Elements by tag name okay so as you all can see it has returned that this is a Dom element and the name of the tag is item like I already told you all before Dom applications often start by passing XML into Dom and that is why you see this as your output saying Dom element item at so and so memory location now in case I want to fetch the value of the attributes present in the item tag I can just say print tag name do attributes and then I'll specify the name of the attribute which is name itself and then I'll use the value attribute to print out its value so as you all can see it has returned the value of this attribute which is break first to retrieve the data that's present in these tags you can make use of the data attribute let me just comment the statement out okay to do this I'll just print tag name which is the name of my variable DOT first child and then I'll use the first child method do data and then I'll hit run so as you all can see the text information present in this item tag has been returned so now in case you want to retrieve the names of other items that are present in our XML file all all I'm going to do is remove this subscript from here I'll comment out the statement I'll copy it and then I'll use a subscript over here say one and then I'll hit run so as you all can see the result is paper Dosa which is the second item that's present in my sample XML file in case I want to print out all the elements that are present in my file all I'm going to do is Loop through all the item names that are present in my file and then print it and then print the data present in it okay so as you all can see in the output of here all the item names present in my file have been returned now let's get back to our presentation and see how you can actually retrieve the length of the elements that are present in my file to retrieve the length of the elements I I can make use of the Len function so let's get back to pyam and over here I'll just say print Len of the tag name okay as you all can see it has returned that the length of item tag name is five so I hope everyone's understood this with this we've reached the end of this session I hope you've enjoyed and learned something new goodbye and take care e e e e
Original Description
๐ด ๐๐๐๐ซ๐ง ๐๐ซ๐๐ง๐๐ข๐ง๐ ๐๐๐๐ก๐ง๐จ๐ฅ๐จ๐ ๐ข๐๐ฌ ๐ ๐จ๐ซ ๐ ๐ซ๐๐! ๐๐ฎ๐๐ฌ๐๐ซ๐ข๐๐ ๐ญ๐จ ๐๐๐ฎ๐ซ๐๐ค๐ ๐๐จ๐ฎ๐๐ฎ๐๐ ๐๐ก๐๐ง๐ง๐๐ฅ: https://2ly.link/1w69F
This Edureka video on 'Arrays in Python' will help you establish a strong hold on all the fundamentals in python programming language.
๐ข๐ข ๐๐จ๐ฉ ๐๐ ๐๐ซ๐๐ง๐๐ข๐ง๐ ๐๐๐๐ก๐ง๐จ๐ฅ๐จ๐ ๐ข๐๐ฌ ๐ญ๐จ ๐๐๐๐ซ๐ง ๐ข๐ง ๐๐๐๐ ๐๐๐ซ๐ข๐๐ฌ ๐ข๐ข
โฉ NEW Top 10 Technologies To Learn In 2024 - https://www.youtube.com/watch?v=vaLXPv0ewHU
๐ด Subscribe to our channel to get video updates. Hit the subscribe button above: https://goo.gl/6ohpTV
๐ด ๐๐๐ฎ๐ซ๐๐ค๐ ๐๐ง๐ฅ๐ข๐ง๐ ๐๐ซ๐๐ข๐ง๐ข๐ง๐ ๐๐ง๐ ๐๐๐ซ๐ญ๐ข๐๐ข๐๐๐ญ๐ข๐จ๐ง๐ฌ
๐ต DevOps Online Training: http://bit.ly/3VkBRUT
๐ AWS Online Training: http://bit.ly/3ADYwDY
๐ต React Online Training: http://bit.ly/3Vc4yDw
๐ Tableau Online Training: http://bit.ly/3guTe6J
๐ต Power BI Online Training: http://bit.ly/3VntjMY
๐ Selenium Online Training: http://bit.ly/3EVDtis
๐ต PMP Online Training: http://bit.ly/3XugO44
๐ Salesforce Online Training: http://bit.ly/3OsAXDH
๐ต Cybersecurity Online Training: http://bit.ly/3tXgw8t
๐ Java Online Training: http://bit.ly/3tRxghg
๐ต Big Data Online Training: http://bit.ly/3EvUqP5
๐ RPA Online Training: http://bit.ly/3GFHKYB
๐ต Python Online Training: http://bit.ly/3Oubt8M
๐ต GCP Online Training: http://bit.ly/3VkCzS3
๐ Microservices Online Training: http://bit.ly/3gxYqqv
๐ต Data Science Online Training: http://bit.ly/3V3nLrc
๐ CEHv12 Online Training: http://bit.ly/3Vhq8Hj
๐ต Angular Online Training: http://bit.ly/3EYcCTe
๐ด ๐๐๐ฎ๐ซ๐๐ค๐ ๐๐จ๐ฅ๐-๐๐๐ฌ๐๐ ๐๐จ๐ฎ๐ซ๐ฌ๐๐ฌ
๐ต DevOps Engineer Masters Program: http://bit.ly/3Oud9PC
๐ Cloud Architect Masters Program: http://bit.ly/3OvueZy
๐ต Data Scientist Masters Program: http://bit.ly/3tUAOiT
๐ Big Data Architect Masters Program: http://bit.ly/3tTWT0V
๐ต Machine Learning Engineer Masters Program: http://bit.ly/3AEq4c4
๐ Business Intelligence Mas