开发者

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

开发者 https://www.devze.com 2023-03-16 12:51 出处:网络
from xml.dom.minidom import Document def generateXML(): # Create the minidom document doc = Document()
 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")
  开发者_JAVA技巧  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()

Now when i print it -- Its just returning <?xml version="1.0" ?> , I actually want the whole xml which i created. Please help


You are appending the elements the wrong way. It's parentnode.appendChild(childnode), you've written it as childnode.appendChild(parentnode)

0

精彩评论

暂无评论...
验证码 换一张
取 消