首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用lxml模块解析XML时出现未知错误

使用lxml模块解析XML时出现未知错误
EN

Stack Overflow用户
提问于 2016-11-29 20:19:33
回答 2查看 892关注 0票数 0

我现在正在使用Python 101进行学习,在其中一个例子中,我得到了一个错误,并且不知道如何修复它--我的代码与书中的100%相同(已经检查了3次),它仍然输出这个错误。以下是代码:

代码语言:javascript
运行
复制
from lxml import etree

def parseXML(xmlFile):
    """
    Parse the xml
    """
    with open(xmlFile) as fobj:
        xml = fobj.read()

    root = etree.fromstring(xml)

    for appt in root.getchildren():
        for elem in appt.getchildren():
            if not elem.text:
                text = 'None'
            else:
                text = elem.text
            print(elem.tag + ' => ' + text)

if __name__ == '__main__':
    parseXML('example.xml')

下面是xml文件(与书中的相同):

代码语言:javascript
运行
复制
<?xml version="1.0" ?>
<zAppointments reminder-"15">
    <appointment>
        <begin>1181251600</begin>
        <uid>0400000008200E000</uid>
        <alarmTime>1181572063</alarmTime>
        <state></state>
        <location></location>
        <duration>1800</duration>
        <subject>Bring pizza home</subject>
    </appointment>
    <appointment>
        <begin>1234567890</begin>
        <duration>1800</duration>
        <subject>Check MS office webstie for updates</subject>
        <state>dismissed</state>
        <location></location>
        <uid>502fq14-12551ss-255sf2</uid>
     </appointment>
</zAppointments>

编辑: Sry,对我的第一篇文章非常兴奋,以至于我实际上忘了输入错误代码。

代码语言:javascript
运行
复制
Traceback (most recent call last):
  File "/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py", line 21, in <module>
    parseXML('example.xml')
  File "/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py", line 10, in parseXML
    root = etree.fromstring(xml)
  File "src/lxml/lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src/lxml/lxml.etree.c:77737)
  File "src/lxml/parser.pxi", line 1830, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:116674)
  File "src/lxml/parser.pxi", line 1711, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:115220)
  File "src/lxml/parser.pxi", line 1051, in lxml.etree._BaseParser._parseUnicodeDoc (src/lxml/lxml.etree.c:109345)
  File "src/lxml/parser.pxi", line 584, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:103584)
  File "src/lxml/parser.pxi", line 694, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:105238)
  File "src/lxml/parser.pxi", line 624, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:104147)
lxml.etree.XMLSyntaxError: Specification mandate value for attribute reminder-, line 2, column 25

谢谢你帮忙!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-29 20:24:17

xml中唯一的错误可以在这里找到:<zAppointments reminder-"15">,应该是:<zAppointments reminder="15">

将来,可以在网上找到验证xml的有用工具。例如:https://www.xmlvalidation.com/

票数 2
EN

Stack Overflow用户

发布于 2016-11-29 20:33:53

错误可能在

代码语言:javascript
运行
复制
<zAppointments reminder-"15">

对于下一个验证,请尝试使用xmllint

代码语言:javascript
运行
复制
xmllint --valid --noout example.xml
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40875073

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档