Error: lxml.etree.XMLSyntaxError: expected '>'

Question:

I have this XML data in a string:

<?xml version="1.0" encoding="UTF-8"?>
<class name="C" kind ="concrete">
    <inheritance>
        <from name="A" privacy="public" />
        <from name="B" privacy="public" />
    </inheritance>
    <private>
        <methods>
            <method name="C" type="C" scope="instance">
                <arguments></arguments>
        </methods>
    </private>
</class>

I want to find some elements using xpath. As far this is my code:

utf8_parser = etree.XMLParser(encoding='utf-8')
root = etree.fromstring(string.encode('utf-8'), parser=utf8_parser)
somelist = root.findall(xpathString)

I got this error:

root = etree.fromstring(stringOutput.string.encode('utf-8'), parser=utf8_parser)
  File "lxml.etree.pyx", line 3032, in lxml.etree.fromstring (src/lxml/lxml.etree.c:68106)
  File "parser.pxi", line 1785, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:102455)
  File "parser.pxi", line 1673, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:101284)
  File "parser.pxi", line 1074, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:96466)
  File "parser.pxi", line 582, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:91275)
  File "parser.pxi", line 683, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:92461)
  File "parser.pxi", line 622, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:91757)
lxml.etree.XMLSyntaxError: expected '>', line 11, column 11

I thought the problem could be with double quotes in the string. Is it possible? How should the proper code to find the elements using xpath look like?

Asked By: T.Poe

||

Answers:

The double quotes delimiting attribute values are entirely fine, but the method element missing an end tag is not. Here is your XML repaired to be well-formed:

<?xml version="1.0" encoding="UTF-8"?>
<class name="C" kind ="concrete">
    <inheritance>
        <from name="A" privacy="public" />
        <from name="B" privacy="public" />
    </inheritance>
    <private>
        <methods>
            <method name="C" type="C" scope="instance">
                <arguments></arguments>
            </method>
        </methods>
    </private>
</class>
Answered By: kjhughes

XML validation using an online service gives you immediate help with validation errors, a web search for ‘online xml validator’ will yield many options.

lxml.etree.XMLSyntaxError: expected '>', line 11, column 11
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: methods line 4 and private, line 12, column 19

are both related to the tag <method ...> it is missing a closing tag:

        <methods>
            <method name="C" type="C" scope="instance">
                <arguments></arguments>
            </method>
        </methods>

Here are two example online validators:
codebeautify xml validator, simple explanation of each error.
w3c xml validation service, more advanced, validating for xml and html.

Answered By: dank8
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.