擅长:python、mysql、java
<p>问题是这不是有效的XML。在XML中,<code>&</code>符号总是以<a href="http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references" rel="nofollow">entity reference</a>开头,比如<code>&#1234;</code>代表字符<code>U+04D2</code>(又名<code>Ӓ</code>),字符<code>&quot;</code>,或者文档/DTD/schema中定义的某个自定义实体。*</p>
<p>如果你想把一个字面值<code>&</code>放入一个字符串中,你必须用别的东西来代替它,通常是<code>&amp;</code>,它是与字元的字符实体引用。在</p>
<p>因此,如果您确定文档中没有实际的实体引用,只有未转义的和号,您可以非常简单地修复它:</p>
<pre><code>with open('metaWeb.xml') as f:
xml = f.read().replace('&', '&amp;')
tree = etree.fromstring(xml)
</code></pre>
<p>然而,一个更好的解决方案,如果可能的话,就是修复任何生成错误XML的程序。在</p>
<hr/>
<p>*这有点误导,这是事实;数字字符引用实际上不是实体引用。另外,像<code>&quot;</code>或<code>&amp;</code>这样的字符实体引用与任何其他具有替换文本的引用相同,这些实体恰好是由基于XML/HTML的dtd隐式定义的。但是<code>lxml</code>与大多数XML软件一样,使用术语“实体引用”比标准稍微宽泛一些。在</p>