2017-06-22 69 views
0

我正在嘗試爲思科ISE Guest API創建一些訪客用戶XML文件(使用Pythong etree),並被困在如何創建名稱空間部分(請參見下面的粗體)中。如何構建這個XML命名空間?

**<ns4:guestuser description="ERS Example user " id="123456789" name="guestuser" xmlns:ers="ers.ise.cisco.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns4="identity.ers.ise.cisco.com">** 

element... 
element... 
</ns4:guestuser> 

下面是我的Python程序的一部分...我做錯了嗎?我如何插入從描述=「ERS示例用戶」開始的長字符串的命名空間..?

任何建議/幫助將非常感激..

from xml.etree import ElementTree as ET 

def buildTree(name): 
root = ET.Element("ns4:guestuser") 
<snip> 
<snip> 

tree.write("user1.xml", xml_declaration=True, encoding='utf-8', method="xml") 

回答

0

您可以使用attrib parameter添加元素的屬性:

>>> attributes = {"description":"ERS Example user ", "id":"123456789", "name":"guestuser", " xmlns:ers":"ers.ise.cisco.com", "xmlns:xs":"http://www.w3.org/2001/XMLSchema", "xmlns:ns4":"identity.ers.ise.cisco.com"} 
>>> root = ET.Element("ns4:guestuser", attrib=attributes) 
>>> print ET.tostring(root) 
<ns4:guestuser xmlns:ers="ers.ise.cisco.com" description="ERS Example user " id="123456789" name="guestuser" xmlns:ns4="identity.ers.ise.cisco.com" xmlns:xs="http://www.w3.org/2001/XMLSchema" /> 
+0

@ har07 ....你讓我的日子....那工作!!! ....乾杯... – Michael