简体   繁体   English

让 lxml 对 XML 片段进行编码

[英]Getting lxml to encode an XML fragment

I'm having a bit of an issue with encoding some XML using lxml.我在使用 lxml 编码一些 XML 时遇到了一些问题。 Specifically I had this xml fragment:具体来说,我有这个 xml 片段:

<p>first para</p><p><ul><li>item1</li><li>item2</li></ul></p>

In my code I was using:在我使用的代码中:

_before = fragment_fromstring(_before)

However, this gave me:然而,这给了我:

<p>first para</p><p/><ul><li>item1</li><li>item2</li></ul>

This is because in HTML you don't wrap paragraphs around lists.这是因为在 HTML 中,您不会在列表周围环绕段落。 However, I can and need to do that in this particular case in XML.但是,在这种特殊情况下,我可以而且需要在 XML 中这样做。

I have tried:我试过了:

_before = et.fromstring(_before)

however, that is giving me this error:然而,这给了我这个错误:

lxml.etree.XMLSyntaxError: error parsing attribute name, line 1, column 972

Looking through the docs, I can't see a way to tell lxml, I'm giving you an xml fragment, and I don't care at the minute if I have a root element (I'm actually building all of that as I go).查看文档,我看不到告诉 lxml 的方法,我给你一个 xml 片段,我现在不在乎我是否有一个根元素(我实际上正在构建所有这些我去)。

Anybody know how I might pass lxml an XML code fragment and get an encoded lxml fragment?有人知道我如何通过 lxml 传递一个 XML 代码片段并获得一个编码的 lxml 片段吗?

I'm now using the parser.feed option, which is in fact also showing this parsing error, but looking at my XML written to this point, I now see why.我现在正在使用 parser.feed 选项,它实际上也显示了这个解析错误,但是看看我写到这里的 XML,我现在明白为什么了。 Seems that some of my hand written XML (there's a reason not to do this, I know), has a couple of minor issues that when I used the fragment_fromstring was actually correcting for me.似乎我的一些手写 XML(我知道是有理由不这样做的)有几个小问题,当我使用 fragment_fromstring 时实际上是在为我纠正这些小问题。 Fixing those errors and using parser.feed seems to have resolved this issue.修复这些错误并使用 parser.feed 似乎已经解决了这个问题。

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

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