0

我试图从中提取作者:

<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)

返回空字符串。

怎么了?谢谢。

4

3 回答 3

2

使用ElementTree,正如@Martijn Pieters 所建议的那样:

from xml.etree import ElementTree
tree = ElementTree.fromstring('<icon><author>Author Name</author></icon>')
print tree.find('author').text

更多示例: http: //www.doughellmann.com/PyMOTW/xml/etree/ElementTree/parse.html

于 2013-02-07T21:59:55.217 回答
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
于 2013-02-07T22:01:35.867 回答
1

使用 lxml 轻松实现:

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

返回“作者姓名”

于 2013-02-07T22:03:10.853 回答