繁体   English   中英

在解析 xml 时没有得到任何类型,即使它有一些价值,我该如何解决这个问题?

[英]while parsing xml getting none type even it has some value,how can i solve this issue?

输入
如您所见,我正在尝试解析的 xml 代码具有名称价值,那么它怎么可能是 NoneType?

import xml.etree.ElementTree as ET
input='''
  <stuff>
    <user>
      <user x='2' >
        <id>001</id>
        <name>chuck</name>
     </user>
     <user x ='7'>
       <id>009</id>
       <name>brent</name>
       </user>
     </user>
    </stuff>

     '''
stuff=ET.fromstring(input)
print(stuff.find('name').text)

output

AttributeError             Traceback (most recent call last)
<ipython-input-7-f10e349a116e> in <module>
     16      '''
     17 stuff=ET.fromstring(input)
---> 18 print(stuff.find('name').text)

AttributeError: 'NoneType' object has no attribute 'text'

您需要指定正确的xpath 语法以获取子名称标签。

尝试这个:

print(stuff.find('.//name').text)

结果:

chuck

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM