2011-04-20 62 views
2

我遇到了xml.dom.minidom python包的奇怪問題。我生成一個文檔,用從終端獲取的數據填充文檔。有時這些數據包含終端控制字符。當我在minidom.toprettyxml()文本數據節點中存儲這樣的字符時,一切看起來都很好,但是,生成的文檔不是有效的XML。Python xml.dom.minidom生成無效的XML?

有誰知道爲什麼minidom允許生成無效文檔?這是與「迷你」部分連接嗎?

這裏是提取的示例代碼(有一些系統信息太):

Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from xml.dom import minidom 
>>> impl = minidom.getDOMImplementation() 
>>> doc = impl.createDocument(None, "results", None) 
>>> root = doc.firstChild 
>>> outString = "test "+chr(1) #here goes control character 
>>> root.appendChild(doc.createTextNode(outString)) 
<DOM Text node "'test \x01'"> 
>>> doc.toprettyxml(encoding="utf-8") 
'<?xml version="1.0" encoding="utf-8"?>\n<results>\n\ttest \x01\n</results>\n' 
>>> with open("/tmp/outfile", "w") as f: 
...  f.write(doc.toprettyxml(encoding="utf-8")) 
... 
>>> doc2 = minidom.parse("/tmp/outfile") 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse 
    return expatbuilder.parse(file) 
    File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 924, in parse 
    result = builder.parseFile(fp) 
    File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile 
    parser.Parse(buffer, 0) 
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 3, column 6 
>>> open("/tmp/outfile","r").readlines() 
['<?xml version="1.0" encoding="utf-8"?>\n', '<results>\n', '\ttest \x01\n', '</results>\n'] 
>>> 
+0

嘗試將您的文本移動到CDATA部分。 – 2011-04-20 09:44:10

+0

可能有各種解決方法,我甚至比CDATA更好,但問題是:爲什麼minidom會生成無效的XML?這是一個錯誤還是一個功能? (可能與miniidom的「ligthness」連接) – 2011-04-20 12:50:30

回答

1

望着用於_write_data代碼只逃脫&號,斜線和支架:

def _write_data(writer, data): 
    "Writes datachars to writer." 
    data = data.replace("&", "&amp;").replace("<", "&lt;") 
    data = data.replace("\"", "&quot;").replace(">", "&gt;") 
    writer.write(data) 

正如你猜測,minidom並不是完全可靠的實現(例如,它缺少名稱空間的實現)。