python parse xml
时间:2010-05-12 来源:terry-xcb
#!/usr/bin/python
import sys
from xml.dom import minidom def findNode( in_parent, in_nodeName):
"""
check wether the child node exist from paraent xml tree
"""
if in_parent:
for node in in_parent.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.nodeName == in_nodeName:
return node if __name__=="__main__":
xml_fd = minidom.parse('./b.xml') TargetBD = findNode(xml_fd, 'TargetBD') if(TargetBD == None):
sys.exit(0) softwareReleaseVersion = TargetBD.getAttribute('softwareReleaseVersion')
commandScriptName = TargetBD.getAttribute('commandScriptName')
buildDescriptorId = TargetBD.getAttribute('buildDescriptorId') print softwareReleaseVersion
print commandScriptName
print buildDescriptorId buildElement = findNode(TargetBD, 'buildElement')
if(buildElement == None):
sys.exit(0) ElementCounts = TargetBD.getElementsByTagName("buildElement")
for element in ElementCounts:
softwareReleaseType = element.getAttribute('softwareReleaseType')
softwareReleaseId = element.getAttribute('softwareReleaseId')
softwareArchiveName = element.getAttribute('softwareArchiveName')
print softwareReleaseType
print softwareReleaseId
print softwareArchiveName
======================================================================== xml ================================ <?xml version="1.0" encoding="UTF-8"?>
<TargetBD softwareReleaseVersion='Q3 1.2-0 RNXENVQ3 1.2-0' buildDescriptorId='Q3 1.2-0 RNXENVQ3 1.2-0 Q3060602' commandScriptName='SCRIPT.ZIP'> <buildElement softwareReleaseType='Full Release' softwareReleaseId='Q3060600' softwareArchiveName='Q3060600.ZIP' softwareReleaseDate='18/09/2007' newFlag='.T.'>
</buildElement> <buildElement softwareReleaseType='Change Delivery' softwareReleaseId='Q3060601' softwareArchiveName='Q3060601.ZIP' softwareReleaseDate='20/09/2007' newFlag='T'>
</buildElement> <buildElement softwareReleaseType='Change Delivery' softwareReleaseId='Q3060602' softwareArchiveName='Q3060602.ZIP' softwareReleaseDate='26/09/2007' newFlag='T'>
</buildElement> </TargetBD>
from xml.dom import minidom def findNode( in_parent, in_nodeName):
"""
check wether the child node exist from paraent xml tree
"""
if in_parent:
for node in in_parent.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.nodeName == in_nodeName:
return node if __name__=="__main__":
xml_fd = minidom.parse('./b.xml') TargetBD = findNode(xml_fd, 'TargetBD') if(TargetBD == None):
sys.exit(0) softwareReleaseVersion = TargetBD.getAttribute('softwareReleaseVersion')
commandScriptName = TargetBD.getAttribute('commandScriptName')
buildDescriptorId = TargetBD.getAttribute('buildDescriptorId') print softwareReleaseVersion
print commandScriptName
print buildDescriptorId buildElement = findNode(TargetBD, 'buildElement')
if(buildElement == None):
sys.exit(0) ElementCounts = TargetBD.getElementsByTagName("buildElement")
for element in ElementCounts:
softwareReleaseType = element.getAttribute('softwareReleaseType')
softwareReleaseId = element.getAttribute('softwareReleaseId')
softwareArchiveName = element.getAttribute('softwareArchiveName')
print softwareReleaseType
print softwareReleaseId
print softwareArchiveName
======================================================================== xml ================================ <?xml version="1.0" encoding="UTF-8"?>
<TargetBD softwareReleaseVersion='Q3 1.2-0 RNXENVQ3 1.2-0' buildDescriptorId='Q3 1.2-0 RNXENVQ3 1.2-0 Q3060602' commandScriptName='SCRIPT.ZIP'> <buildElement softwareReleaseType='Full Release' softwareReleaseId='Q3060600' softwareArchiveName='Q3060600.ZIP' softwareReleaseDate='18/09/2007' newFlag='.T.'>
</buildElement> <buildElement softwareReleaseType='Change Delivery' softwareReleaseId='Q3060601' softwareArchiveName='Q3060601.ZIP' softwareReleaseDate='20/09/2007' newFlag='T'>
</buildElement> <buildElement softwareReleaseType='Change Delivery' softwareReleaseId='Q3060602' softwareArchiveName='Q3060602.ZIP' softwareReleaseDate='26/09/2007' newFlag='T'>
</buildElement> </TargetBD>
相关阅读 更多 +