IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> Read xml file example(Minidom) -> 正文阅读

[Python知识库]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

<item>
    <record>
        <name>Jameson</name>
        <phone>(080) 78168241</phone>   
        <email>cursus.in.hendrerit@ipsumdolor.edu</email>
        <country>South Africa</country>
    </record>

    <record>
        <name>Colton</name>
        <phone>(026) 53458662</phone>
        <email>non@idmagna.ca</email>
        <country>Libya</country>
    </record>

    <record>
        <name>Dillon</name>
        <phone>(051) 96790901</phone>
        <email>Aliquam.ornare@Etiamlaoreetlibero.ca</email>
        <country>Madagascar</country>
    </record>
  
    <record>
        <name>Channing</name>
        <phone>(014) 98829753</phone>
        <email>faucibus.Morbi.vehicula@aliquamarcu.co.uk</email>
        <country>Korea, South</country>
    </record>
</item>

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

  1. <name>,
  2. <phone>,
  3. <email>, and
  4. <country>

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 <record> tags
records = file.getElementsByTagName("record")

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

for record in records:
    #access <name> and <phone> 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.

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2021-10-07 13:48:31  更:2021-10-07 13:50:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/15 17:59:30-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码