Home > Backend Development > Python Tutorial > Python reading and writing XML files case sharing

Python reading and writing XML files case sharing

巴扎黑
Release: 2017-08-06 17:59:34
Original
2070 people have browsed it

This article mainly introduces relevant information about examples of reading and writing XML files in Python, examples of Python generating XML files and Python reading XML. Friends in need can refer to the following

Detailed explanation of Python Examples of reading and writing XML files

Python generates XML files


from xml.dom import minidom

# 生成XML文件方式
def generateXml():
  impl = minidom.getDOMImplementation()

  # 创建一个xml dom
  # 三个参数分别对应为 :namespaceURI, qualifiedName, doctype
  doc = impl.createDocument(None, None, None)

  # 创建根元素
  rootElement = doc.createElement('Pythons')

  # 为根元素添加10个子元素
  for pythonId in range(10):
    # 创建子元素
    childElement = doc.createElement('python')
    # 为子元素添加id属性
    childElement.setAttribute('id', str(pythonId))

    # 将子元素追加到根元素中
    rootElement.appendChild(childElement)
    print(childElement.firstChild.data)

  # 将拼接好的根元素追加到dom对象
    doc.appendChild(rootElement)

  # 打开test.xml文件 准备写入
  f = open('test.xml', 'a')
  # 写入文件
  doc.writexml(f, addindent=' ', newl='\n')
  # 关闭
  f.close()

# 执行生成xml方法
generateXml()
Copy after login

python reads XML file


from xml.dom.minidom import parse

# 获取 python节点下得所有id属性
def getTagId():

  # 获取test.xml文档对象
  doc = parse("test.xml")

  for node in doc.getElementsByTagName("python"):
    # 获取标签ID属性
    value_str = node.getAttribute("id")
    # 打印输出
    print(value_str)

# 获取属性ID
getTagId()
Copy after login

Running result – the generated xml file is as follows

Run results – read the xml file as follows

The above is the detailed content of Python reading and writing XML files case sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template