Python 当使用lxml时,是否可以在没有名称空间属性的情况下呈现XML?
我使用lxml生成一些XML,并生成如下节点:Python 当使用lxml时,是否可以在没有名称空间属性的情况下呈现XML?,python,lxml,Python,Lxml,我使用lxml生成一些XML,并生成如下节点: <QBXML xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" py:pytype="TREE"> <configuration description="Network Li
<QBXML xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
py:pytype="TREE">
<configuration description="Network Lists" name="acl.conf">
<network-lists>
</network-lists>
</configuration>
以及:
这些自定义属性正在杀死Quickbooks的解析器。我可以让LXML在没有自定义内容的情况下进行渲染吗?看起来如下所示,请注意:
objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)
或者,如果使用lxml>=2.3.2(谢谢@Pedru):
如果你正在使用
etree.fromstring(xml_response)
然后这样做:
xml_response.replace(' xmlns:', ' xmlnamespace:').replace(' xmlns=', ' xmlnamespace=')
避免解析名称空间如果您想要嵌套XML,可以执行以下操作:
from lxml import objectify
doc = objectify.ElementMaker(annotate=False)
doc = (objectify.E.configuration(getattr(objectify.E,'networklists'),name="acl.conf",description="Network Lists"))
objectify.deannotate(doc,cleanup_namespaces=True)
具有自定义属性的输出如下所示:
<QBXML xmlns:py="http://codespeak.net/lxml/objectify/pytype" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
py:pytype="TREE">
<configuration description="Network Lists" name="acl.conf">
<network-lists>
</network-lists>
</configuration>
从lxml 2.3.2版开始,您可以将
cleanup\u namespaces=True
传递到objectify.deannotate()
(无需调用etree.cleanup\u namespaces()
)