gpt4 book ai didi

python - 在python中访问xml中的子节点

转载 作者:行者123 更新时间:2023-12-01 06:17:35 26 4
gpt4 key购买 nike

如何检索以下 XML 中的 type 值

 <info><category>Flip</category><info>2</info><type>Tree</type></info>

最佳答案

使用ElementTree:

import xml.etree.ElementTree as E
e = E.parse("test.xml")
print(e.find("type").text)

使用 minidom:

import xml.dom.minidom
d = xml.dom.minidom.parse("test.xml")
print(d.getElementsByTagName("type")[0].firstChild.data)

使用BeautifulSoup :

from BeautifulSoup import BeautifulStoneSoup
soup = BeautifulStoneSoup(open("test.xml"))
print(soup.find("type").text)

关于python - 在python中访问xml中的子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2378834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com