簡體   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