Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用lxml模块解析XML时出现未知错误_Python_Python 3.x_Lxml - Fatal编程技术网

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

Python 使用lxml模块解析XML时出现未知错误,python,python-3.x,lxml,Python,Python 3.x,Lxml,我现在正在学习使用Python 101,在其中一个示例中,我遇到了一个错误,但不知道如何修复它-我的代码与书中的代码完全相同(已经检查了3次),它仍然输出这个错误。 代码如下: from lxml import etree def parseXML(xmlFile): """ Parse the xml """ with open(xmlFile) as fobj: xml = fobj.read() root = etree.from

我现在正在学习使用Python 101,在其中一个示例中,我遇到了一个错误,但不知道如何修复它-我的代码与书中的代码完全相同(已经检查了3次),它仍然输出这个错误。 代码如下:

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文件(与书中相同):


1181251600
040000008200E000
1181572063
1800
把比萨饼带回家
1234567890
1800
检查MS office网站以获取更新
解雇
502fq14-12551ss-255sf2
编辑:Sry,对我的第一篇帖子太兴奋了,以至于我忘了写错误代码

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
回溯(最近一次呼叫最后一次):
文件“/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py”,第21行,在
parseXML('example.xml')
parseXML格式的文件“/home/michal/Desktop/nauka programowania/python 101/parsing_with_lxml.py”,第10行
root=etree.fromstring(xml)
文件“src/lxml/lxml.etree.pyx”,第3213行,位于lxml.etree.fromstring(src/lxml/lxml.etree.c:77737)中
lxml.etree.\u parseMemoryDocument(src/lxml/lxml.etree.c:116674)中的文件“src/lxml/parser.pxi”,第1830行
文件“src/lxml/parser.pxi”,第1711行,在lxml.etree.\u parseDoc(src/lxml/lxml.etree.c:115220)中
lxml.etree.\u BaseParser.\u parseUnicodec(src/lxml/lxml.etree.c:109345)中的文件“src/lxml/parser.pxi”,第1051行
文件“src/lxml/parser.pxi”,第584行,在lxml.etree.\u ParserContext.\u handleParseResultDoc(src/lxml/lxml.etree.c:103584)中
文件“src/lxml/parser.pxi”,第694行,在lxml.etree.\u handleParseResult(src/lxml/lxml.etree.c:105238)中
lxml.etree中的文件“src/lxml/parser.pxi”,第624行。raiseParserError(src/lxml/lxml.etree.c:104147)
lxml.etree.XMLSyntaxError:属性提醒的规范强制值,第2行,第25列

谢谢你的帮助

在这里可以找到xml中唯一的错误:
,应该是:

将来,可以在线找到验证xml的有用工具。
例如:

在这里可以找到xml中唯一的错误:
,应该是:

将来,可以在线找到验证xml的有用工具。 例如:

错误可能在

<zAppointments reminder-"15">
错误可能在

<zAppointments reminder-"15">

补充,抱歉,对我在这里的第一篇帖子感到兴奋而忘记了。补充,抱歉,对我在这里的第一篇帖子感到兴奋而忘记了。现在工作正常,下次要使用xmlvalidation站点,非常感谢!现在工作正常,下次要使用xmlvalidation站点,非常感谢!
xmllint --valid --noout example.xml