简体   繁体   English

错误:lxml.etree.XMLSyntaxError:预期为'>'

[英]Error: lxml.etree.XMLSyntaxError: expected '>'

I have this XML data in a string: 我在字符串中有此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>

I want to find some elements using xpath. 我想找到一些使用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? 使用xpath查找元素的正确代码应该如何?

The double quotes delimiting attribute values are entirely fine, but the method element missing an end tag is not. 限定属性值的双引号是完全可以的,但是缺少end标记的method元素则不是。 Here is your XML repaired to be well-formed: 这是将您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM