簡體   English   中英

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

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

我剛剛發現了lxml.objectify ,它看起來很容易閱讀/編寫簡單的XML文件。

首先,使用lxml.objectify是一個好主意嗎? 例如,它是否已經成熟並且仍在開發中,並可能在將來推出?

其次,如何防止objectify在下面的輸出中添加xmlns:py="http://codespeak.net/lxml/objectify/pytype" py:pytype="str"這樣的標記?


輸入:config.xml

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

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)

產量

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

如此處指出的那樣: 使用lxml時,是否可以不使用名稱空間屬性來呈現XML? ,這足以防止此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