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 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 . In my program, I iterate through each "virtualMachine" node and create a python object called TargetVM based on it. I want to basically create a list of "fileToBeSent" and "scriptToBeRanPython" objects / tuples, and make that an attribute of my TargetVM class.
This is a method that is called from the constructor of a TargetVM, that parses the rest of the 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? 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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.