python解析xml文件

  1. 加载和读取xml文件

    import xml.dom.minidom
    doc = xml.dom.minidom.parse(xmlfile)
    
  2. 获取xml文档对象(对子节点和节点node都适用)

    root = doc.documentElement
    
  3. 节点属性

    root.nodeName  # 每个节点都有它的 nodeName,nodeValue, nodeType属性;
    root.nodeValue  # nodeValue 是节点的值,只对本文本节点有效;
    文本节点:
    Element节点下面没有别的节点,只有文本的话
    txt_node = Element节点.firstChild
    txt_node.data 或者 txt_node.nodeValue都是可以获取文本
    root.nodeType  # 节点类型;
    root.ELEMENT_NODE
    
  4. 属性值的获取、修改、删除

    root.getAttribute(attributeName)  # 获取 xml 节点属性值;
    root.setAttribute(attributeName, value)  # 修改或添加 xml 节点属性值;
    root.getElementsByTagName(TagName)  # 根据标签获取 xml 节点对象集合
    root.removeAttribute(attributeName)  # 删除 xml 节点属性值;
    
  5. 子节点的访问

    root.childNodes  # 获取子节点列表;
    root.childNodes[index].nodeValue  # 获取 xml 节点值;
    c # 访问第一个节点(相当于 root.childNodes[0]);
    root.childNodes[0].data  # 获得文本值;
    
  6. 删除和生成节点

    # 删除 node 节点下面的子节点 childnode_in_node
    node.removeChild(childnode_in_node) 
    # 生成节点  # 文本节点.createTextNode('xxxxx')
    node.createElement('activity')  
    
  7. pass

通过xml.dom.minidom解析xml文件

"""
    <collection shelf="New Arrivals">
        <movie title="Enemy Behind">
            <type>War, Thriller</type>
            <format>DVD</format>
            <year>2003</year>
            <rating>PG</rating>
            <stars>10</stars>
            <description>Talk about a US-Japan war</description>
        </movie>
        <movie title="Transformers">
            <type>Anime, Science Fiction</type>
            <format>DVD</format>
            <year>1989</year>
            <rating>R</rating>
            <stars>8</stars>
            <description>A schientific fiction</description>
        </movie>
        <movie title="Trigun">
            <type>Anime, Action</type>
            <format>DVD</format>
            <episodes>4</episodes>
            <rating>PG</rating>
            <stars>10</stars>
            <description>Vash the Stampede!</description>
        </movie>
        <movie title="Ishtar">
            <type>Comedy</type>
            <format>VHS</format>
            <rating>PG</rating>
            <stars>2</stars>
            <description>Viewable boredom</description>
        </movie>

"""

# 通过minidom解析xml文件
import xml.dom.minidom as xmldom

# get file object
doc = xmldom.parse(r'movie.xml')  # <class 'xml.dom.minidom.Document'>
# get element object
root = doc.documentElement  # <class 'xml.dom.minidom.Element'>
node1 = root.getElementsByTagName("movie")  # <class 'xml.dom.minicompat.NodeList'>
# get tab attribute
print(node1[0].getAttribute("title"))  # Enemy Behind

movie = root.getElementsByTagName("movie")
print(movie[0].nodeName)  # movie
print(movie[0].nodeType)  # 1
print(movie[0].nodeValue)  # None
print(movie[0].lastChild)  # <DOM Text node "'
	'">

year_list = root.getElementsByTagName("year")
print(year_list[0].firstChild.data)  # 2003
print(year_list[0].nodeValue)  # None
for i in range(len(year_list)):
    print(year_list[i].lastChild)

# <DOM Text node "'2003'">
# <DOM Text node "'1989'">
# <DOM Text node "'2014'">
# <DOM Element: p2 at 0x10792cc28>

print(year_list[0].firstChild.nodeValue)  # 2003      
name age
shanpao S12
原文地址:https://www.cnblogs.com/he-qing-qing/p/12781849.html