简体   繁体   English

使用lxml.objectify删除“ xmlns:py…”

[英]Remove “xmlns:py…” with lxml.objectify

I just discovered lxml.objectify which seems nice and easy for reading/writing simple XML files. 我刚刚发现了lxml.objectify ,它看起来很容易阅读/编写简单的XML文件。

Firstly, is it a good idea to use lxml.objectify ? 首先,使用lxml.objectify是一个好主意吗? For instance is it mature and still developed and likely to be available in the future? 例如,它是否已经成熟并且仍在开发中,并可能在将来推出?

Secondly, how do I prevent objectify from addding markup like xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str" in the output below ?. 其次,如何防止objectify在下面的输出中添加xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str"这样的标记?


Input : config.xml 输入:config.xml

<?xml version="1.0" encoding="utf-8"?>
<Test>
  <MyElement1>sdfsdfdsfd</MyElement1>
</Test>

Code

from lxml import etree, objectify

with open('config.xml') as f:
    xml = f.read()
root = objectify.fromstring(xml)

root.Information = 'maybe'

print etree.tostring(root, pretty_print=True)

Output 产量

<Test>
  <MyElement1>sdfsdfdsfd</MyElement1>
  <Information xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str">maybe</Information>
</Test>

As pointed out here : When using lxml, can the XML be rendered without namespace attributes? 如此处指出的那样: 使用lxml时,是否可以不使用名称空间属性来呈现XML? , this is enough to prevent this xmlns markup to appear : ,这足以防止此xmlns标记出现:

objectify.deannotate(root, xsi_nil=True)
etree.cleanup_namespaces(root)

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

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