Python 如何检索使用minidom创建的xml? 来自xml.dom.minidom导入文档 def generateXML(): #创建minidom文档 doc=文件() #创建基本元素 discover=doc.createElement(“discover”) 追加子文档(发现) #创建主元素 主机=doc.createElement(“主机”) host.appendChild(发现) #创建主元素 ip=doc.createElement(“ip”) ip.appendChild(主机) #为元素分配IP地址 ipaddrr=doc.createTextNode('10.193.184.72') ip.appendChild(ipaddrr) #创建主元素 hostname=doc.createElement(“主机名”) hostname.appendChild(主机) #为元素分配主机名 hostname\u value=doc.createTextNode('darknight') hostname.appendChild(主机名\u值) #创建主元素 ostype=doc.createElement(“ostype”) ostype.appendChild(主机) #使用ostype分配元素 ostype_value=doc.createTextNode('mac')) 追加子对象(ostype_值) return doc.toprettyxml()文件 打印generateXML()

Python 如何检索使用minidom创建的xml? 来自xml.dom.minidom导入文档 def generateXML(): #创建minidom文档 doc=文件() #创建基本元素 discover=doc.createElement(“discover”) 追加子文档(发现) #创建主元素 主机=doc.createElement(“主机”) host.appendChild(发现) #创建主元素 ip=doc.createElement(“ip”) ip.appendChild(主机) #为元素分配IP地址 ipaddrr=doc.createTextNode('10.193.184.72') ip.appendChild(ipaddrr) #创建主元素 hostname=doc.createElement(“主机名”) hostname.appendChild(主机) #为元素分配主机名 hostname\u value=doc.createTextNode('darknight') hostname.appendChild(主机名\u值) #创建主元素 ostype=doc.createElement(“ostype”) ostype.appendChild(主机) #使用ostype分配元素 ostype_value=doc.createTextNode('mac')) 追加子对象(ostype_值) return doc.toprettyxml()文件 打印generateXML(),python,xml,minidom,Python,Xml,Minidom,现在当我打印它时——它刚刚返回,我实际上想要我创建的整个xml。请帮助您以错误的方式添加元素。它是parentnode.appendChild(childnode),您将其编写为childnode.appendChild(parentnode)您以错误的方式追加元素。它是parentnode.appendChild(childnode),您将其编写为childnode.appendChild(parentnode) from xml.dom.minidom import Document

现在当我打印它时——它刚刚返回
,我实际上想要我创建的整个xml。请帮助

您以错误的方式添加元素。它是
parentnode.appendChild(childnode)
,您将其编写为
childnode.appendChild(parentnode)
您以错误的方式追加元素。它是
parentnode.appendChild(childnode)
,您将其编写为
childnode.appendChild(parentnode)

 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()