ขณะนี้ คุณไม่สามารถเพิ่มเนมสเปซไปยังเอกสาร XML ได้โดยตรง เนื่องจากยังไม่รองรับในแพ็คเกจ Python xml ที่สร้างขึ้น ดังนั้น คุณจะต้องเพิ่มเนมสเปซเป็นแอตทริบิวต์ปกติให้กับแท็ก ตัวอย่างเช่น
import xml.dom.minidom
doc = xml.dom.minidom.Document()
element = doc.createElementNS('http://hello.world/ns', 'ex:el')
element.setAttribute("xmlns:ex", "http://hello.world/ns")
doc.appendChild(element)
print(doc.toprettyxml()) นี่จะให้เอกสารกับคุณ
<?xml version="1.0" ?> <ex:el xmlns:ex="http://example.net/ns"/>