Python基于dom操作xml數(shù)據(jù)的方法示例
本文實(shí)例講述了Python基于dom操作xml數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:
1、xml的內(nèi)容為del.xml,如下
<?xml version="1.0" encoding="utf-8"?> <catalog> <maxid>4</maxid> <login username="pytest" passwd='123456'> <caption>Python</caption> <item id="4"> <caption>test</caption> </item> </login> <item id="2"> <caption>Zope</caption> </item> </catalog>
2、python的代碼如下
# -*- coding:utf-8 -*- #! python3 #1、獲得標(biāo)簽屬性 print("#1、獲得標(biāo)簽屬性") import xml.dom.minidom dom = xml.dom.minidom.parse("del.xml") #打開xml文檔 root = dom.documentElement #得到xml文檔 print("nodeName:",root.nodeName) # print("nodeValue:",root.nodeValue) print("nodeType:",root.nodeType) print("ELEMENT_NODE:",root.ELEMENT_NODE) #2、獲得子標(biāo)簽 print("#2、獲得子標(biāo)簽") bb = root.getElementsByTagName('maxid') print(type(bb)) print(bb) b = bb[0] print(b.nodeName) print(b.nodeValue) #3、獲取標(biāo)簽屬性值 print("#3、獲取標(biāo)簽屬性值") itemlist = root.getElementsByTagName('login') item =itemlist[0] print(item.getAttribute("username")) print(item.getAttribute("passwd")) itemlist = root.getElementsByTagName('item') item = itemlist[0] #通過在itemlist中的位置區(qū)分 print(item.getAttribute("id")) item_1 = itemlist[1] #通過在itemlist中的位置區(qū)分 print(item_1.getAttribute("id")) #4、獲得標(biāo)簽對之間的數(shù)據(jù) print("#4、獲得標(biāo)簽對之間的數(shù)據(jù)") itemlist1 = root.getElementsByTagName('caption') item1 = itemlist1[0] print(item1.firstChild.data) item2 = itemlist1[1] print(item2.firstChild.data) #5總結(jié) # minidom.parse(filename) # 加載讀取XML文件 # # doc.documentElement # 獲取XML文檔對象 # # node.getAttribute(AttributeName) # 獲取XML節(jié)點(diǎn)屬性值 # # node.getElementsByTagName(TagName) # 獲取XML節(jié)點(diǎn)對象集合 # # node.childNodes # 返回子節(jié)點(diǎn)列表。 # # node.childNodes[index].nodeValue # 獲取XML節(jié)點(diǎn)值 # # node.firstChild # # 訪問第一個(gè)節(jié)點(diǎn)。等價(jià)于pagexml.childNodes[0]
3、運(yùn)行結(jié)果如下:
#1、獲得標(biāo)簽屬性
nodeName: catalog
nodeValue: None
nodeType: 1
ELEMENT_NODE: 1
#2、獲得子標(biāo)簽
<class 'xml.dom.minicompat.NodeList'>
[<DOM Element: maxid at 0x1dad800>]
maxid
None
#3、獲取標(biāo)簽屬性值
pytest
123456
4
2
#4、獲得標(biāo)簽對之間的數(shù)據(jù)
Python
test
運(yùn)行結(jié)果截圖:
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- Python數(shù)據(jù)提取-lxml模塊
- Python爬蟲基于lxml解決數(shù)據(jù)編碼亂碼問題
- Python 解析簡單的XML數(shù)據(jù)
- Python xml、字典、json、類四種數(shù)據(jù)類型如何實(shí)現(xiàn)互相轉(zhuǎn)換
- Python 讀取xml數(shù)據(jù),cv2裁剪圖片實(shí)例
- Python大數(shù)據(jù)之使用lxml庫解析html網(wǎng)頁文件示例
- Python 3.x基于Xml數(shù)據(jù)的Http請求方法
- 詳細(xì)解讀Python中解析XML數(shù)據(jù)的方法
- python與xml數(shù)據(jù)的交互詳解
相關(guān)文章
Python一行代碼實(shí)現(xiàn)ChatGPT接入微信機(jī)器人
這篇文章主要為大家介紹了Python一行代碼實(shí)現(xiàn)ChatGPT接入微信機(jī)器人示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03一文帶你了解CNN(卷積神經(jīng)網(wǎng)絡(luò))
CNN是神經(jīng)網(wǎng)絡(luò)中的一種,它的權(quán)值共享網(wǎng)絡(luò)結(jié)構(gòu)使之更類似于生物神經(jīng)網(wǎng)絡(luò),降低了網(wǎng)絡(luò)模型的復(fù)雜度,減少了權(quán)值的數(shù)量。本文主要講解了CNN(卷積神經(jīng)網(wǎng)絡(luò))的基礎(chǔ)內(nèi)容,想了解更多的小伙伴可以看一看這篇文章2021-09-09推薦10款最受Python開發(fā)者歡迎的Python IDE
這篇文章收集了一些對開發(fā)者非常有幫助的,最好的10款Python IDE,包括Vim,Eclipse with PyDev,Sublime Text,PyCharm等知明Python開發(fā)工具2018-09-09如何把python項(xiàng)目部署到linux服務(wù)器
這篇文章主要介紹了如何把python項(xiàng)目部署到linux服務(wù)器,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08