2013-02-07 93 views
0

我試圖從這個作者:讀取XML與

<icon> 
    <tags> 
     <tag>steam</tag> 
    </tags> 
    <author>Author Name</author> 
    <authorwebsite>http://www.domain.com/</authorwebsite> 
    <license> 
     Creative Commons (Attribution-Noncommercial-Share Alike 3.0 Unported) 
    </license> 
    <licensewebsite>http://creativecommons.org/licenses/by-nc-sa/3.0/</licensewebsite> 
    <iconset>Name</iconset> 
    <iconsetid>slug</iconsetid> 
    <attribution/> 
    <additionalsizes> 
     <icon> 
      <id>99633</id> 
      <size>128</size> 
      <tags/> 
      <image> 
       http://url1 
      </image> 
     </icon> 
     <icon> 
      <id>99633</id> 
      <size>256</size> 
      <tags/> 
      <image> 
       http://url2 
      </image> 
     </icon> 
     <icon> 
      <id>99633</id> 
      <size>512</size> 
      <tags/> 
      <image> 
       http://url3 
      </image> 
     </icon> 
    </additionalsizes> 
</icon> 

我想:

name = dom.getElementsByTagName('author') 
print name[0].firstChild.nodeValue 

AttributeError的: 'NoneType' 對象有沒有屬性 '的nodeValue'

而且:

name = dom.getElementsByTagName('author') 
print " ".join(t.nodeValue for t in name[0].childNodes if t.nodeType == t.TEXT_NODE) 

返回空stri NG。

怎麼了?謝謝。

+3

您綁定到DOM API嗎?我會轉而使用ElementTree API;更容易。請參閱http://docs.python.org/2/library/xml.etree.elementtree.html –

+0

您使用的是什麼XML包? – isedev

+0

@isedev我從xml.dom.minidom導入解析 –

回答

2

你忘了從圖標標籤

from xml.dom.minidom import parse 
dom = parse('test.xml') 
icon = dom.getElementsByTagName('icon')[0] 
author = icon.getElementsByTagName('author')[0] 
print author.firstChild.nodeValue 
1

易peasy裏面得到作者lxml:

from lxml import etree 
dom=etree.fromstring(XML_DOC) 
dom.xpath('/icon/author/text()')[0] 

返回'作者姓名'