Python - 使用Minidom进行xml解析 - 如何遍历每个<parent>并获取<parent>的<child>列表?</parent> </child> </parent>

时间:2015-03-11 18:03:30

标签: python xml python-2.7 minidom

道歉,如果这是一个基本问题,但我已经坚持了一段时间,并没有找到可理解的迷你文档。我有这个xml文件:

<?xml version="1.0"?>
<targetConfiguration>




    <virtualMachine>
        <boxNameUppercase>CENTOS65</boxNameUppercase>
        <boxNameLowercase>centos65</boxNameLowercase>
        <memoryMegabytes>2048</memoryMegabytes>
        <numCPUs>2</numCPUs>

        <installLocation>/home/user/VM_deployer_vms/CENTOS65_Auto/</installLocation>
        <diskSpaceGigabytes>10</diskSpaceGigabytes>
        <Vagrantfile>/Vagrantfiles/Vagrantfile.centos65.overwritable</Vagrantfile> 


        <fileToBeSent>
            <source>yaml file on system</source>
            <destination>bamboo agent location</destination>
        </fileToBeSent>

        <fileToBeSent>
            <source>yaml file on system</source>
            <destination>bamboo agent location</destination>
        </fileToBeSent>

        <scriptToBeRanPython>
            <paramaters>-autodetectOS -noPrompt -name</paramaters>          
            <sourceName>......agent_install.py </sourceName>
            <destinationName>SOME LOCATION ON THE MACHINE</destinationName>
        </scriptToBeRanPython>

    </virtualMachine>



    <virtualMachine>
        <boxNameUppercase>CENTOS64</boxNameUppercase>
        <boxNameLowercase>centos64</boxNameLowercase>
        <memoryMegabytes>2048</memoryMegabytes>
        <numCPUs>2</numCPUs>

        <installLocation>/home/user/VM_deployer_vms/CENTOS64_Auto/</installLocation>
        <diskSpaceGigabytes>10</diskSpaceGigabytes>
        <Vagrantfile>/Vagrantfiles/Vagrantfile.centos65.overwritable</Vagrantfile> 

        <fileToBeSent>
            <source>yaml file on system</source>
            <destination>bamboo agent location</destination>
        </fileToBeSent>

        <fileToBeSent>
            <source>yaml file on system</source>
            <destination>bamboo agent location</destination>
        </fileToBeSent>

        <scriptToBeRanPython>
            <paramaters>-autodetectOS -noPrompt -name</paramaters>          
            <sourceName>......agent_install.py </sourceName>
            <destinationName>SOME LOCATION ON THE MACHINE</destinationName>
        </scriptToBeRanPython>

    </virtualMachine>

</targetConfiguration>

我的任何问题基本上都是“fileToBeSent”和“scriptToBeRanPython”节点。在我的程序中,我遍历每个“virtualMachine”节点并基于它创建一个名为TargetVM的python对象。我想基本上创建一个“fileToBeSent”和“scriptToBeRanPython”对象/元组的列表,并使其成为我的TargetVM类的属性。

这是一个从TargetVM的构造函数调用的方法,它解析xml的其余部分:

def buildConfiguration(self, inputFile):
        doc = minidom.parse(inputFile)

        #virtualMachine
        vm_xml_elements = doc.getElementsByTagName("virtualMachine")
        for vm in vm_xml_elements:
            myTargetVM=TargetVM()   

            #mandatory xml nodes    
            try:
                myTargetVM.diskSpaceGigabytes = vm.getElementsByTagName("diskSpaceGigabytes")[0].firstChild.nodeValue     
                myTargetVM.installLocation = vm.getElementsByTagName("installLocation")[0].firstChild.nodeValue 
                myTargetVM.vagrantfile = vm.getElementsByTagName("Vagrantfile")[0].firstChild.nodeValue 
            except:
                addFailure("XML error for virtualMachine. You've left out some mandatory tags. ")           


            #optional xml nodes
            try:


                myTargetVM.boxNameUppercase = vm.getElementsByTagName("boxNameUppercase")[0].firstChild.nodeValue
                myTargetVM.boxNameLowercase= vm.getElementsByTagName("boxNameLowercase")[0].firstChild.nodeValue
                myTargetVM.memoryMegabytes = vm.getElementsByTagName("memoryMegabytes")[0].firstChild.nodeValue        
                myTargetVM.numCPUs = vm.getElementsByTagName("numCPUs")[0].firstChild.nodeValue 



            except:
                addWarning("You left out some optional XML tags when specifying a vagrantBox.")         
            self.TargetVMList.append(myTargetVM)

如何修改它以使其与上述xml一起使用?我尝试过添加类似的内容:

                printDebug( "   Adding commands to "+ VM.getBoxName())              
                commandsTagList = vm.getElementsByTagName("virtualMachine")             
                for command in commandsTagList:
                    commandString = command.getElementsByTagName("scriptToBeRanPython")[0].firstChild.nodeValue
                    myTargetVM.commandList.append(commandString)        
                    printDebug( "      added command '" + commandString + "' to "+VM.getBoxName())  

但它返回一个空列表。谁能帮我?非常感谢。

1 个答案:

答案 0 :(得分:0)

您必须从源名称,参数标记中获取值。请看以下示例:

doc = minidom.parse('data.xml')
commandsTagList = doc.getElementsByTagName("virtualMachine")
for command in commandsTagList:
    scriptToBeRanPython = command.getElementsByTagName("scriptToBeRanPython")[0]
    parameter = scriptToBeRanPython.getElementsByTagName("paramaters")[0].firstChild.nodeValue
    source = scriptToBeRanPython.getElementsByTagName("sourceName")[0].firstChild.nodeValue
    destination = scriptToBeRanPython.getElementsByTagName("destinationName")[0].firstChild.nodeValue
    commandString = source + ' ' + parameter + ' ' + destination
    print commandString

输出:

......agent_install.py  -autodetectOS -noPrompt -name SOME LOCATION ON THE MACHINE
......agent_install.py  -autodetectOS -noPrompt -name SOME LOCATION ON THE MACHINE