網站首頁 Python教程 正文
1. 示例用法
參照官方文檔,創建country_data.xml測試文檔,內容如下:
<?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>
使用如下代碼,將數據讀出,打印
from xml.etree.ElementTree
data = ElementTree.ElementTree(file='country_data.xml')
country_list = data.findall('country') #找到所有名為‘country'的tag,返回一個Element對象列表。
for country in country_list:
name = country.attrib.get('name', '')
print name, ' ',
for item in country:
if item.tag == 'neighbor':
name = item.attrib.get('name', '')
direction = item.attrib.get('direction', '')
print '{0} ({1})'.format(name, direction), ' ',
else:
print item.text, ' ',
print ''
其中
data = ElementTree.ElementTree(file='country_data.xml')
獲得一個ElementTree對象,也可以使用
tree = ElementTree.parse('country_data.xml')
Element對象具有如下屬性和操作
elem.tag | 這個Element對象的名字(tag) |
elem.text | 文檔內容 |
elem.attrib | 屬性值字典 |
elem.tail | 與屬性一起存儲的其他數據 |
elem[n] 返回elem的第n個子元素
elem[n] = new_elem 將elem的第n個子元素更改為不同的元素new_elem
del elem[n] 刪除子元素
len(elem) 子元素的數量
elem.find(path)
elem.getchildren() 按文檔順序返回所有子元素
elem.items()將所有元素的屬性值以(name, value)對列表形式返回
遇到非法格式的xml
ExpatError: no element found
bad.xml為空文檔時,內容如下:
<?xml version="1.0"?>
執行如下python代碼,遇到xml.parser.expat.ExpatError異常:
import xml.etree.ElementTree as ET
ET.parse('bad.xml')
xml.parsers.expat.ExpatError: no element found: line 3, column 0
ExpatError: mismatched tag
bad.xml中找不到對應結束標記符時,內容如下:
<?xml version="1.0"?> <note> </Note>
因為區分大小寫,所以</Note> 不能作為<note>的結束標記。
xml.parsers.expat.ExpatError: mismatched tag: line 3, column 2
ExpatError: not well-formed(invalid token)
bad.xml中屬性值未包含在雙引號(")之中時,遇到如下異常:
<?xml version="1.0"?> <note id=hello> </note>
bad.xml中非法符號,在"if salary < 1000 then"語句的‘<',如下:
<?xml version="1.0"?> <note id="hello"> if salary < 1000 then </note
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 2, column 9
原文鏈接:https://blog.csdn.net/wangst4321/article/details/8697838
相關推薦
- 2021-11-29 Docker部署前后端分離項目的實現示例_docker
- 2022-04-22 nvm切換node版本后提示npm : 無法將“npm”項識別為 cmdlet、函數、腳本文件或可運
- 2023-03-28 python中向二維數組中添加整行或者增列元素問題_python
- 2023-03-27 python中end="?"的含義及說明_python
- 2022-07-21 提高新手寫代碼效率的Emmet插件怎么使用
- 2022-05-02 python庫h5py入門詳解_python
- 2022-06-10 Flutter實現心動的動畫特效_Android
- 2022-04-01 Kubeadm:如何解決kubectl get cs顯示scheduler Unhealthy,co
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支