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 "'\n\t'">

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM