[英]Python: Access the next element in XML using ElementTree
我的XML
文件沒有標簽,這使得解析不太友好。 我試圖找到一個元素並更改它:
...
<dict>
<key>Arguments</key>
<dict>
<key>TestArchive</key
<string>testArchive.tar</string>
</dict>
....
</dict>
...
我想做的是獲取並更改“ testArchive.tar”字符串,因為該字符串可能每次都會更改,因此我將TestArchive用作錨點
from xml.etree import ElementTree
with open ('file.xml', 'rt') as f:
tree = ElementTree.parse(f)
for item in tree.iter():
if item.text == "TestArchive":
....
如何獲得下一個元素? 在我的情況下,應該是我要尋找的商品?
您需要在關鍵元素上使用getnext()
。 這將獲得下一個兄弟姐妹。
if item.text == "TestArchive":
name = item.getnext()
print name.text
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.