網站首頁 編程語言 正文
XML 被設計用來傳輸和存儲數據。
HTML 被設計用來顯示數據。
XML 指可擴展標記語言(eXtensible Markup Language)。
可擴展標記語言(英語:Extensible Markup Language,簡稱:XML)是一種標記語言,是從標準通用標記語言(SGML)中簡化修改出來的。它主要用到的有可擴展標記語言、可擴展樣式語言(XSL)、XBRL和XPath等。
直接上代碼,拿來就可用。
首先需要準備一個測試??xml?
??文件,我這個文件名字為??text.xml?
?;
<data> <country name="Liechtenstein"> <rank>yunweijia</rank> <year>2022</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E" /> <neighbor name="Switzerland" direction="W" /> </country> <country name="Singapore"> <rank>yunweijia</rank> <year>2023</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N" /> </country> <country name="Panama"> <rank>yunweijia</rank> <year>2024</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W" /> <neighbor name="Colombia" direction="E" /> </country> </data>
然后使用以下代碼來進行修改;
import xml.etree.ElementTree as ET def change_one_xml(xml_path, xml_dw, update_content): # 打開xml文檔 doc = ET.parse(xml_path) root = doc.getroot() # 查找修改路勁 sub1 = root.find(xml_dw) # 修改標簽內容 sub1.text = update_content # 保存修改 doc.write(xml_path) # 欲修改文件 xml_path = r'test.xml' # 修改文件中的xpath定位 xml_dw = './/country[@name="Singapore"]/year' # 想要修改成什么內容 update_content = '9999' change_one_xml(xml_path, xml_dw, update_content)
運行完畢之后,我們可以看到源文件內容變成了;
<data> <country name="Liechtenstein"> <rank>yunweijia</rank> <year>2022</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E" /> <neighbor name="Switzerland" direction="W" /> </country> <country name="Singapore"> <rank>yunweijia</rank> <year>9999</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N" /> </country> <country name="Panama"> <rank>yunweijia</rank> <year>2024</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W" /> <neighbor name="Colombia" direction="E" /> </country> </data>
原文鏈接:https://blog.51cto.com/u_12386780/5479899
相關推薦
- 2022-08-04 python中可以發生異常自動重試庫retrying_python
- 2022-02-28 獲取元素的寬度,高度
- 2022-05-06 基于python分享極坐標下的幾類典型曲線_python
- 2022-04-20 Python設計模式結構型組合模式_python
- 2022-05-10 FactoryBean配置文件定義的 類型 調用時返回 不同的類型
- 2022-09-03 pandas如何將datetime64[ns]轉為字符串日期_python
- 2023-05-29 python怎樣判斷一個數值(字符串)為整數_python
- 2022-07-04 C#操作配置文件app.config、web.config增刪改_C#教程
- 最近更新
-
- 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同步修改后的遠程分支