2013-03-12 71 views
29

我使用ElementTree在Python中生成XML文檔,但tostring函數在轉換爲純文本時不包含XML declaration如何使用xml.etree.ElementTree編寫XML聲明

from xml.etree.ElementTree import Element, tostring 

document = Element('outer') 
node = SubElement(document, 'inner') 
node.NewValue = 1 
print tostring(document) # Outputs "<outer><inner /></outer>" 

我需要我的字符串包括以下XML聲明:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 

然而,似乎沒有被這樣做的任何記錄的方式。

是否有合適的方法來呈現ElementTree中的XML聲明?

回答

50

我很驚訝地發現,似乎沒有要與ElementTree.tostring()一種方式。但是,您可以使用ElementTree.ElementTree.write()到XML文檔寫入文件假貨:

from io import BytesIO 
from xml.etree import ElementTree as ET 

document = ET.Element('outer') 
node = ET.SubElement(document, 'inner') 
et = ET.ElementTree(document) 

f = BytesIO() 
et.write(f, encoding='utf-8', xml_declaration=True) 
print(f.getvalue()) # your XML file, encoded as UTF-8 

this question。即使那樣,我也不認爲你可以在沒有自己預先寫好的情況下得到你的「獨立」屬性。

+0

爲什麼在這裏定義「節點」變量? – 2015-06-11 11:01:08

+3

感謝這條線et.write(f,encoding ='utf-8',xml_declaration = True)保存了我的日子 – 2016-10-20 09:16:49

15

我會使用lxml(請參閱http://lxml.de/api.html)。

然後,您可以:

from lxml import etree 
document = etree.Element('outer') 
node = etree.SubElement(document, 'inner') 
print(etree.tostring(document, xml_declaration=True)) 
3

我最近遇到此問題,代碼的一些挖後,我發現下面的代碼片段的功能ElementTree.write

def write(self, file, encoding="us-ascii"): 
    assert self._root is not None 
    if not hasattr(file, "write"): 
     file = open(file, "wb") 
    if not encoding: 
     encoding = "us-ascii" 
    elif encoding != "utf-8" and encoding != "us-ascii": 
     file.write("<?xml version='1.0' encoding='%s'?>\n" % 
    encoding) 
    self._write(file, self._root, encoding, {}) 

定義因此,答案是,如果需要XML頭寫入到文件,設置除utf-8us-ascii之外的其他參數encoding,例如UTF-8

+0

這雖然很脆弱,但它似乎不起作用(編碼可能更低 - 在此之前)。此外,'ElementTree.ElementTree.write()'被記錄爲具有'xml_declaration'參數(請參閱接受的答案)。但是'ElementTree。tostring()'沒有這個參數,這是在原始問題中提出的方法。 – 2015-04-14 07:31:47

0

我會用ET

try: 
    from lxml import etree 
    print("running with lxml.etree") 
except ImportError: 
    try: 
     # Python 2.5 
     import xml.etree.cElementTree as etree 
     print("running with cElementTree on Python 2.5+") 
    except ImportError: 
     try: 
      # Python 2.5 
      import xml.etree.ElementTree as etree 
      print("running with ElementTree on Python 2.5+") 
     except ImportError: 
      try: 
       # normal cElementTree install 
       import cElementTree as etree 
       print("running with cElementTree") 
      except ImportError: 
       try: 
        # normal ElementTree install 
        import elementtree.ElementTree as etree 
        print("running with ElementTree") 
       except ImportError: 
        print("Failed to import ElementTree from any known place") 

document = etree.Element('outer') 
node = etree.SubElement(document, 'inner') 
print(etree.tostring(document, encoding='UTF-8', xml_declaration=True)) 
0

這工作,如果你只想打印。得到一個錯誤,當我嘗試把它發送到一個文件...

import xml.dom.minidom as minidom 
import xml.etree.ElementTree as ET 
from xml.etree.ElementTree import Element, SubElement, Comment, tostring 

def prettify(elem): 
    rough_string = ET.tostring(elem, 'utf-8') 
    reparsed = minidom.parseString(rough_string) 
    return reparsed.toprettyxml(indent=" ") 
2

If you include the encoding='utf8', you will get an XML header

xml.etree.ElementTree.tostring寫了一個XML編碼聲明與編碼= 'utf-8'

樣品的Python 2代碼:

import xml.etree.ElementTree as ElementTree 

tree = ElementTree.ElementTree(
    ElementTree.fromstring('<xml><test>123</test></xml>') 
) 
root = tree.getroot() 

print 'without:' 
print ElementTree.tostring(root, method='xml') 
print 
print 'with:' 
print ElementTree.tostring(root, encoding='utf8', method='xml') 

輸出:

without: 
<xml><test>123</test></xml> 

with: 
<?xml version='1.0' encoding='utf8'?> 
<xml><test>123</test></xml> 
+0

在Python 3中,轉義字符將在打印時顯示在聲明中。 '<?xml version = \'1.0 \'encoding = \'utf8 \'?>' – 2017-10-30 16:05:46