2011-07-01 5 views
1
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をしたいです。助けてくださいminidomを使って作成されたxmlを取り出すには?今

答えて

1

あなたは間違った方法で要素を追加しています。それはparentnode.appendChild(childnode)だと書いてあります。childnode.appendChild(parentnode)

+0

ありがとうございました。 – HOT

関連する問題