小编RAS*_*GLE的帖子

AttributeError:“Element”对象没有属性“findAll”

我正在尝试使用命名空间解析 XML,XML 看起来像

<DATA xmlns="http://example.com/nspace/DATA/1.0"  xmlns:UP="http://example.com/nspace/UP/1.1" col_time_us="14245034321452862">
<UP:IN>...</UP:IN>
<UP:ROW>
     <sampleField>...</sampleField>                
</UP:ROW>
<UP:ROW>
     <sampleField>...</sampleField>                
</UP:ROW>
.
. 
.
</DATA>
Run Code Online (Sandbox Code Playgroud)

当我使用下面的代码来解析XML时

tree=ET.parse(fileToParse);
root=tree.getRoot();
namespaces = {'UP':'http://example.com/nspace/DATA/1.0'}
for data in root.findAll('UP:ROW',namespaces):
        hour+=1
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

AttributeError: 'Element' object has no attribute 'findAll'
Run Code Online (Sandbox Code Playgroud)

当我尝试遍历 root 的子级并打印标签时,我得到的{http://example.com/nspace/DATA/1.0}ROW是标签而不仅仅是 ROWS。

我想找到 ROW 元素并提取 SampleField 标记的值。有人可以指导我我可能做错了什么吗?

python xml namespaces findall attributeerror

4
推荐指数
1
解决办法
9651
查看次数

标签 统计

attributeerror ×1

findall ×1

namespaces ×1

python ×1

xml ×1