0

am curently learning using Python 101 and in one of examples I'm getting an error and have no clue how to fix it - my code is 100% same as in the book (checked it 3 times already) and it still outputs this error. Here is the code:

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

and here is xml file (it's the same as in the book):

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

EDITED: Sry, got so excited about my first post that I actually forgot to put the error code.

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

Thanks for help!!

1
  • Added, sorry, got to excited about my first post here and forgot about it. Commented Nov 29, 2016 at 20:24

2 Answers 2

2

The only error in the xml can be found here: <zAppointments reminder-"15">, should be: <zAppointments reminder="15">.

In the future useful tools for validating xml can be found online. Here for example: https://www.xmlvalidation.com/

Sign up to request clarification or add additional context in comments.

1 Comment

Works fine now, gonna use that xmlvalidation site next time, thx a lot!
0

Error may be in

<zAppointments reminder-"15">

For next validation try to use xmllint:

xmllint --valid --noout example.xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.