错误:lxml.etree.XMLSyntaxError:expected'>'

时间:2016-04-16 18:46:29

标签: python xml xpath lxml

我将这个XML数据放在一个字符串中:

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

我想使用xpath找到一些元素。到目前为止,这是我的代码:

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

我收到了这个错误:

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

我认为问题可能在于字符串中的双引号。可能吗?如何使用xpath查找元素的正确代码如何?

1 个答案:

答案 0 :(得分:2)

分隔属性值的双引号完全没问题,但缺少结束标记的method元素却没有。以下是您修复的XML格式良好:

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