use*_*462 3 python xml elementtree
我的问题来自另一个stackoverflow问题: - "如何在Python中获取xml文件的根节点?"
from xml.etree import ElementTree as ET
path = 'C:\cool.xml'
et = ET.parse ( path )
root = et.getroot()
Run Code Online (Sandbox Code Playgroud)
当我提取并打印根标签时,我收到: -
<Element 'root' at 0x1234abcd>
Run Code Online (Sandbox Code Playgroud)
然后我想检查根标签是否有某个标题,如何只提取标签名称?
如果我尝试:
if root == "root":
print 'something'
Run Code Online (Sandbox Code Playgroud)
它不起作用,所以我假设我需要将'root'转换为文本或类似的东西?我是Python的新手.
您应该能够使用该tag函数来获取节点的名称.
from xml.etree import ElementTree as ET
path = 'C:\cool.xml'
et = ET.parse ( path )
root = et.getroot()
if root.tag == "root":
print "I'm the root"
Run Code Online (Sandbox Code Playgroud)