python - finding a missing tags -
i make test on xml file find place specific tag missing ( tag 'terminal'), test don't work well
from xml.dom import minidom xmldoc = minidom.parse('c:\\test\mydoc.xml') #printing number of blocs in xml file itemlist = xmldoc.getelementsbytagname('aclinesegment') print('************') s in itemlist : if s.childnodes['name'].value == 'terminal': print s.childnodes['name'].value
here exemple of xml file:
<aclinesegment name="t261" description="" aliasname=""> <link_conducting pathb=""/> <terminal name="t1" description="" aliasname=""> <link_terminal pathb=""/> </terminal> <terminal name="t2" description="" aliasname=""> <link_terminal pathb=""/> </terminal> </aclinesegment> <aclinesegment name="t262" description="" aliasname=""> <link_conducting pathb=""/> <terminal name="t1" description="" aliasname=""> <link_terminal pathb=""/> </terminal> <terminal name="t2" description="" aliasname=""> <link_terminal pathb=""/> </terminal> </aclinesegment> <aclinesegment name="t263" description="" aliasname=""> <link_conducting pathb=""/> </aclinesegment> enter code here
how this:
from xml.dom import minidom xmldoc = minidom.parse('c:\\test\mydoc.xml') #printing number of blocs in xml file itemlist = xmldoc.getelementsbytagname('aclinesegment') item in itemlist: found = false child in item.childnodes: if child.nodename == 'terminal': found = true if not found: print item.getattribute('name')
this code prints value of name
attribute of every aclinesegment
element not contain terminal
element:
t263
edit: more succint use:
for item in itemlist: if len([x x in item.childnodes if x.nodename == 'terminal']) == 0: print item.getattribute('name')
this code same logic. inner []
part python list comprehension useful.
it creates list of child nodes of type terminal
. if length of list 0 item didn't have any.
Comments
Post a Comment