[英]Python - xml parsing with Minidom - How do I iterate through each <parent> and get a list of <child> for that <parent>?
Apologies if this is a basic question, but I have been stuck on it for a while and haven't found the minidom documentation that understandable. 抱歉,这是一个基本问题,但是我坚持了一段时间,还没有找到可以理解的简短文档。 I have this xml file:
我有这个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>
Any my problem is basically with the "fileToBeSent" and "scriptToBeRanPython" nodes . 我的任何问题基本上都与“ fileToBeSent”和“ scriptToBeRanPython”节点有关。 In my program, I iterate through each "virtualMachine" node and create a python object called TargetVM based on it.
在我的程序中,我遍历每个“ virtualMachine”节点,并基于该节点创建一个名为TargetVM的python对象。 I want to basically create a list of "fileToBeSent" and "scriptToBeRanPython" objects / tuples, and make that an attribute of my TargetVM class.
我想基本上创建一个“ fileToBeSent”和“ scriptToBeRanPython”对象/元组的列表,并将其作为TargetVM类的一个属性。
This is a method that is called from the constructor of a TargetVM, that parses the rest of the xml: 这是从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)
How do I modify this to get it to work with the above xml? 如何修改它以使其与上述xml一起使用? I have tried adding something like:
我尝试添加类似的内容:
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())
but it returns an empty list. 但它返回一个空列表。 Can anyone help me?
谁能帮我? Thanks very much.
非常感谢。
You have to get the values from the sourceName, paramaters tags. 您必须从sourceName,paramaters标记中获取值。 Look at the following example:
看下面的例子:
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
Output: 输出:
......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.