[英]In Python using etree how do I retain the doc type and declaration
我有一個要每天添加的xml文件。 問題是當我讀入文件然后保存時,它將丟失文件頂部的xml聲明。 我正在使用的代碼:
def parseXML():
xmlFile = open(myfile,'r')
parser = etree.XMLParser(strip_cdata=False)
tree = etree.parse(xmlFile, parser)
return tree
xml_curr = parseXML()
xml_root = xml_curr.getroot()
然后,我附加到此:
xml_root.append(new_elements)
抱歉,如果這不太有意義,python是我的新手。
etree.tostring(tree, pretty_print = True, xml_declaration = True, encoding='UTF-8', standalone="yes")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.