Python-使用Minidom进行xml解析-如何迭代每个并获取该的列表?
如果这是一个基本问题,我深表歉意,但我已经在这个问题上纠缠了一段时间,还没有发现minidom文档是可以理解的。我有一个xml文件:Python-使用Minidom进行xml解析-如何迭代每个并获取该的列表?,python,xml,python-2.7,minidom,Python,Xml,Python 2.7,Minidom,如果这是一个基本问题,我深表歉意,但我已经在这个问题上纠缠了一段时间,还没有发现minidom文档是可以理解的。我有一个xml文件: <?xml version="1.0"?> <targetConfiguration> <virtualMachine> <boxNameUppercase>CENTOS65</boxNameUppercase> <boxNameLowercase&g
<?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>
如何修改它以使其与上述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
您好,Vinod,非常感谢您的回答:我想知道getElementsByTagName的节点顺序是否与XML文件相同?据我所知可以,但我想知道你是否有什么意见?我正在考虑用commandToBeRun或其他什么来代替scriptToBeRanPython,显然,按照正确的顺序完成这些任务是很重要的。我不确定我是否正确理解了您的问题。我只能说秩序很重要。
......agent_install.py -autodetectOS -noPrompt -name SOME LOCATION ON THE MACHINE
......agent_install.py -autodetectOS -noPrompt -name SOME LOCATION ON THE MACHINE