アレッ*_*ックス 11 python xml children elementtree
我以这种方式检索XML文档:
import xml.etree.ElementTree as ET
root = ET.parse(urllib2.urlopen(url))
for child in root.findall("item"):
a1 = child[0].text # ok
a2 = child[1].text # ok
a3 = child[2].text # ok
a4 = child[3].text # BOOM
# ...
Run Code Online (Sandbox Code Playgroud)
XML看起来像这样:
<item>
<a1>value1</a1>
<a2>value2</a2>
<a3>value3</a3>
<a4>
<a11>value222</a11>
<a22>value22</a22>
</a4>
</item>
Run Code Online (Sandbox Code Playgroud)
我如何检查a4(在这种特殊情况下,但它可能是任何其他元素)是否有孩子?
jlr*_*jlr 12
你可以尝试list元素上的函数:
>>> xml = """<item>
<a1>value1</a1>
<a2>value2</a2>
<a3>value3</a3>
<a4>
<a11>value222</a11>
<a22>value22</a22>
</a4>
</item>"""
>>> root = ET.fromstring(xml)
>>> list(root[0])
[]
>>> list(root[3])
[<Element 'a11' at 0x2321e10>, <Element 'a22' at 0x2321e48>]
>>> len(list(root[3]))
2
>>> print "has children" if len(list(root[3])) else "no child"
has children
>>> print "has children" if len(list(root[2])) else "no child"
no child
>>> # Or simpler, without a call to list within len, it also works:
>>> print "has children" if len(root[3]) else "no child"
has children
Run Code Online (Sandbox Code Playgroud)
我修改了你的样本,因为root findall上的函数调用item不起作用(因为它findall会搜索直接后代,而不是当前元素).如果您想在之后的工作程序中访问子子文本,您可以:
for child in root.findall("item"):
# if there are children, get their text content as well.
if len(child):
for subchild in child:
subchild.text
# else just get the current child text.
else:
child.text
Run Code Online (Sandbox Code Playgroud)
这将非常适合递归.
我能够找到的最简单的方法是直接使用bool元素的值。这意味着您可以a4按原样在条件语句中使用:
a4 = Element('a4')
if a4:
print('Has kids')
else:
print('No kids yet')
a4.append(Element('x'))
if a4:
print('Has kids now')
else:
print('Still no kids')
Run Code Online (Sandbox Code Playgroud)
运行此代码将打印
No kids yet
Has kids now
Run Code Online (Sandbox Code Playgroud)
一个元素的布尔值不说任何东西text,tail或属性。它仅指示孩子的存在或不存在,这就是原始问题要问的内容。