Tuesday, 15 July 2014

python - How can i add attribute name and Column value from an xml file to Dictionary -



python - How can i add attribute name and Column value from an xml file to Dictionary -

<table name="table1"> <col>comp1</col> <col>compo4_</col> <col>dire3</col> <col>attri4s</col> <col>condition</col> </table> <table name="table2"> <col>compo4_</col> <col>compon7</col> <col>direc9</col> </table>

i have xml file given above.i want add together value of tablename , col no of compo4_ dictionary.i tried next code.but every time iam getting first column value in coltext.

dic={} node in doc.getelementsbytagname('table'): name = node.getattribute('name') value=0 subnode in node.childnodes: if subnode.nodetype == node.element_node , subnode.tagname == "col": colvalue=colvalue+1 coltext = subnode.getelementsbytagname('col') if coltext=='compo4_': componentiddic[table_name]=colvalue

please don't suggest me utilize elementree.because our whole implemenatation using minidom

the coltext seems different you're expecting. seek utilize firstchild attribute.

coltext = subnode.firstchild.nodevalue

my result is

{'table2': 6, 'table1': 2}

python

No comments:

Post a Comment