[英]while parsing xml getting none type even it has some value,how can i solve this issue?
input输入
As you can see guys the xml code that i am trying to parse,has value in name then how can it be NoneType?如您所见,我正在尝试解析的 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 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'
You need to specify the correct xpath syntax to get to the sub-child name tag.您需要指定正确的xpath 语法以获取子名称标签。
Try this:尝试这个:
print(stuff.find('.//name').text)
Result:结果:
chuck
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.