栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Read xml file example(Minidom)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Read xml file example(Minidom)

XML stands for Extensible Markup Language and like HTML, it is also a markup language. In XML, however, we do not use predefined tags but here we can use our own custom tags based on the data we are storing in the XML file.

An XML document is frequently used to share, store, and design information since it can undoubtedly be moved among servers and frameworks. We as a whole know with regards to information, Python is one of the most amazing programming languages to measure and parse.

Fortunately, Python accompanies a Standard XML module that can parse XML documents in Python and furthermore compose information in the XML record. This is called Python XML Parser.

In this parsing XML in python blog, we will stroll through the Python XML minidom and ElemetnTree modules, and figure out how to parse an XML document in Python.

Python XML minidom and ElementTree module

The Python XML module support two sub-modules minidom and ElementTreeto parse an XML record in Python.

The minidom or Minimal DOM module gives a DOM (document Object Model) like construction to parse the XML record, which is like the DOM design of Javascript.

Despite the fact that we can parse an XML record utilizing minidom, ElementTree gives a greatly improved Pythonic approach to parse an XML document in Python.

XML File

For every one of the models in this instructional exercise, we will utilize the demo.xmlfile, which contains the accompanying XML information:

#demo.xml


    
        Jameson
        (080) 78168241   
        cursus.in.hendrerit@ipsumdolor.edu
        South Africa
    

    
        Colton
        (026) 53458662
        non@idmagna.ca
        Libya
    

    
        Dillon
        (051) 96790901
        Aliquam.ornare@Etiamlaoreetlibero.ca
        Madagascar
    
  
    
        Channing
        (014) 98829753
        faucibus.Morbi.vehicula@aliquamarcu.co.uk
        Korea, South
    

In the above example, you can see that the data is nested under custom . The root tag is , which has as a nested tag, which further has 4 more nested tags:

  1. ,
  2. ,
  3. , and

Parse/Read XML document in Python using minidom

minidom is the submodule of the Python standard XML module, which means you do not have to pip install XML to use minidom.

The minidom module parses the XML document in a document Object Model(DOM), whose data can further be extracted using the getElemetsByTagName()function.

Syntax:

from xml.dom import minidom  minidom.parse("filename")

Example:

Let’s grab all the names and phone data from our demo.xml file.

from xml.dom import minidom


#parse xml file
file = minidom.parse('demo.xml')

#grab all  tags
records = file.getElementsByTagName("record")

print("Name------>Phone")

for record in records:
    #access  and  node of every record
    name = record.getElementsByTagName("name")
    phone = record.getElementsByTagName("phone")
    
    #access data of name and phone
    print(name[0].firstChild.data, end="----->")
    print(phone[0].firstChild.data)

Output

Name------>Phone
Jameson----->(080) 78168241
Colton----->(026) 53458662
Dillon----->(051) 96790901
Channing----->(014) 98829753

Conclusion

That summarizes this tutorial on Python XML Parser. As should be obvious, Python gives an inbuild Standard XML module to peruse and parse XML records in Python. It by and large has 2 submodules that can parse an XML document:

  • minidom and
  • ElementTree

The minidom module follows the document Object Model way to deal with parsing an XML record. Then again, the ElementTree module follows the tree-like construction to parse the XML record.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/296006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号