繁体   English   中英

Python-使用Minidom进行xml解析-如何迭代每个 <parent> 并得到一个清单 <child> 为了那个原因 <parent> ?

[英]Python - xml parsing with Minidom - How do I iterate through each <parent> and get a list of <child> for that <parent>?

抱歉,这是一个基本问题,但是我坚持了一段时间,还没有找到可以理解的简短文档。 我有这个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())  

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

您必须从sourceName,paramaters标记中获取值。 看下面的例子:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM