Python - ElementTree无法访问所有孩子

时间:2017-09-12 23:34:42

标签: python xml parsing exception


我正在编写一个小python程序,使用ElementTree从xml文件创建文件夹结构。但是,如果我尝试在根元素上调用findAll(<tagname>),我总会收到错误:&#34;&#39;元素&#39;对象没有属性&#39; findAll&#39;&#34;。但是如果我深入树中并在我的&#34;季节&#34;上调用findAll()方法。标签它工作得很好。是什么导致了这个问题?

以下是我加载xml的方法:

import xml.etree.ElementTree as ET

videodata = ET.parse(<xml-file>).getroot()
for show in videodata.findAll('show'): #throws the above exception
    #do stuff
for season in videodata.find('show').findAll('season'): #throws the above exception
    #do stuff
for episode in videodata.find('show').find('season').findAll('episode'): #works
    #do stuff

这是我的xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<tvshows>
    <show name="" cover="">
        <season number="1"></season>
        <season number="2"></season>
        <season number="3"></season>
        <season number="4"></season>
        <season number="5"></season>
        <season number="6">
            <episode number="1"/>
            <episode number="2"/>
            <episode number="3"/>
            <episode number="4"/>
        </season>
        <season number="7"></season>
    </show>
    <show name="" cover="">
        <season number="1"></season>
        <season number="2"></season>
        <season number="3"></season>
        <season number="4"></season>
        <season number="5"></season>
    </show>
</tvshows>

1 个答案:

答案 0 :(得分:1)

没有方法Sub Alfredo() Dim msg As String Dim VarCase As Range For Each VarCase In ActiveSheet.Range("D:D") If VarCase.Value2 = "John" Or VarCase.Value2 = "Thompson" Or VarCase.Value2 = "Mattson" Then VarCase.ClearContents End If Next VarCase For Each VarCase In ActiveSheet.Range("D:D") If VarCase.Value = "" Then VarCase.EntireRow.Delete End If Next VarCase End Sub ,方法为findAll()(不是小写findall()