Python find element in xml

XML Parsing In Python Using Element Tree Module

XML Parsing In Python

If you are looking for a tutorial that can help you to learn Parsing XML in python using ElementTree Module then you are landed in right place. In XML Parsing In Python tutorial, you will learn to parse xml file in python. So keep reading this tutorial till the end so that you can have enough knowledge of XML Parsing.

XML Parsing In Python – What Is XML And XML Parsing

In this section, you will see some overview of XML and XML parsing so that you can understand the rest of this tutorial. So let’s get’s started.

What Is XML ?

  • XML is short form of Extensible Markup Language.
  • It is similar to HTML(Hyper Text Markup Language) but the big difference is that HTML is Data Presentation whereas XML defines What is the data that are going to be used.
  • It is designed to store and transport data.
  • XML can easily read by humans and machines.

How XML Looks Likes ?

So now you will see how actually the XML looks like. So this is the basic structure of an XML file.

An XML tree starts at a root element and branches from the root to child elements. All elements can have sub elements (child elements)

Читайте также:  Блокировка поля

Now let’s take an example(products.xml) –

What Is XML Parsing ?

Basically parsing means to read informations from a file and split them into pieces by identifying parts of that particular XML file.

XML Parsing In Python – Parsing XML Documents

In this section, we will see how can we read and write to a XML file. To parse XML data in python, we need parsing module. So let’s discuss XML parsing module in python.

ElementTree Module

ElementTree module is used to formats data in a tree structure which is the most natural representation of hierarchical data. The Element data type allows storage the hierarchical data structure in memory.

The Element Tree is a class that can be used to wrap an element structure, and convert it from and to XML. It has the following properties, let’s see what they are.

  • Tag : It is a string that represents the type of data being stored..
  • Attributes: A number of attributes, stored in a Python dictionary.
  • Text String: A text string having informations that needs to be displayed.
  • Tail String: an optional tail string.
  • Child Elements: a number of child elements, stored in a Python sequence.

Parsing XML

We can do parse XML documents using two methods –

Using parse() method

This method takes XML in file format to parse it. So write the following code to use this method.

Источник

Оцените статью