簡體   English   中英

如何檢索使用 minidom 創建的 xml?

[英]How to retrieve the xml which has been created using the minidom?

 from xml.dom.minidom import Document

 def generateXML():

    # Create the minidom document
    doc = Document()

    # Create the <discover> base element
    discover = doc.createElement("discover")
    doc.appendChild(discover)


    # Create the main <host> element
    host = doc.createElement("host")
    host.appendChild(discover)

    # Create the main <ip> element
    ip = doc.createElement("ip")
    ip.appendChild(host)

    # Assign <ip> element with IP address
    ipaddrr = doc.createTextNode('10.193.184.72')
    ip.appendChild(ipaddrr)

    # Create the main <hostname> element
    hostname = doc.createElement("hostname")
    hostname.appendChild(host)

    # Assign <hostname> element with hostname
    hostname_value = doc.createTextNode('darknight')
    hostname.appendChild(hostname_value)

    # Create the main <ostype> element
    ostype = doc.createElement("ostype")
    ostype.appendChild(host)

    # Assign <ostype> element with ostype
    ostype_value = doc.createTextNode('mac')
    ostype.appendChild(ostype_value)

    return doc.toprettyxml()

 print generateXML()

現在當我打印它時——它只是返回<?xml version="1.0"?> ,我實際上想要我創建的整個 xml 。 請幫忙

您以錯誤的方式附加元素。 它是parentnode.appendChild(childnode) ,你把它寫成childnode.appendChild(parentnode)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM