Python - xml 使用 Minidom 解析 - 我如何遍历每个 <parent> 并获得 <child> 的列表 <parent>?
Python - xml parsing with Minidom - How do I iterate through each <parent> and get a list of <child> for that <parent>?
如果这是一个基本问题,我深表歉意,但我已经坚持了一段时间,还没有找到可以理解的 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 class.
的属性
这是一个从 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())
但它 returns 是一个空列表。谁能帮我?非常感谢。
您必须从源名称、参数标签中获取值。看下面的例子:
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
如果这是一个基本问题,我深表歉意,但我已经坚持了一段时间,还没有找到可以理解的 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 class.
的属性这是一个从 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())
但它 returns 是一个空列表。谁能帮我?非常感谢。
您必须从源名称、参数标签中获取值。看下面的例子:
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