I am writing a parser in python to parse an xml file. For that I need to get tag name but when I am doing so, along with tag name I am getting namespace also. Alongwith this, I need to get the first child at many positions which I am unable to find any attribute in this package (Elementtree). I searched a lot but could not find out as how to get only tag name without namespace value and first child also. The trialinput.xml file is as follows:
<?xml version="1.0"?>
<nf:rpc-reply xmlns:nf="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns="http://ift.tt/WgqNKF">
<nf:data>
<show>
<ip>
<interface>
<__XML__BLK_Cmd_ip_show_interface_command_brief>
<__XML__OPT_Cmd_ip_show_interface_command_operational>
<__XML__OPT_Cmd_ip_show_interface_command_vrf>
<__XML__OPT_Cmd_ip_show_interface_command___readonly__>
<__readonly__>
<TABLE_intf>
<ROW_intf>
<vrf-name-out>default</vrf-name-out>
<intf-name>Vlan10</intf-name>
<prefix>9.1.1.1</prefix>
<ip-disabled>FALSE</ip-disabled>
<iod>104</iod>
<proto-state>TRUE</proto-state>
<link-state>TRUE</link-state>
<admin-state>TRUE</admin-state>
</ROW_intf>
<ROW_intf>
<vrf-name-out>default</vrf-name-out>
<intf-name>Vlan23</intf-name>
<prefix>23.1.1.9</prefix>
<ip-disabled>FALSE</ip-disabled>
<iod>103</iod>
<proto-state>TRUE</proto-state>
<link-state>TRUE</link-state>
<admin-state>TRUE</admin-state>
</ROW_intf>
<ROW_intf>
<vrf-name-out>default</vrf-name-out>
<intf-name>Po1</intf-name>
<prefix>4.9.1.2</prefix>
<ip-disabled>FALSE</ip-disabled>
<iod>111</iod>
<proto-state>TRUE</proto-state>
<link-state>TRUE</link-state>
<admin-state>TRUE</admin-state>
</ROW_intf>
<ROW_intf>
<vrf-name-out>default</vrf-name-out>
<intf-name>Po2</intf-name>
<prefix>5.9.1.2</prefix>
<ip-disabled>FALSE</ip-disabled>
<iod>112</iod>
<proto-state>TRUE</proto-state>
<link-state>TRUE</link-state>
<admin-state>TRUE</admin-state>
</ROW_intf>
<ROW_intf>
<vrf-name-out>default</vrf-name-out>
<intf-name>Po3</intf-name>
<prefix>6.9.1.2</prefix>
<ip-disabled>FALSE</ip-disabled>
<iod>113</iod>
<proto-state>TRUE</proto-state>
<link-state>TRUE</link-state>
<admin-state>TRUE</admin-state>
</ROW_intf>
</TABLE_intf>
</__readonly__>
</__XML__OPT_Cmd_ip_show_interface_command___readonly__>
</__XML__OPT_Cmd_ip_show_interface_command_vrf>
</__XML__OPT_Cmd_ip_show_interface_command_operational>
</__XML__BLK_Cmd_ip_show_interface_command_brief>
</interface>
</ip>
</show>
</nf:data>
</nf:rpc-reply>
The code that I am writing is (in progress of completion, has given just to give a gist of what I am trying to get as an output):
import xml.etree.ElementTree as ET
print "before skip()"
def skip(root):
print "rootname.tag in skip == %s" %(root.tag)
if(root.tag == "readonly"): // here if I compare with the namespace then returns the value
print "skip -> if"
return root
else:
print "skip-> else"
root = root[0]
print "new root %s" %root
return skip(root)
xmlDoc = ET.parse("trialinput.xml")
dict = {}
print "accessing the root"
root = xmlDoc.getroot()
print "rootname == %s" %root.tag
pointerOfReadonly = skip(root)
print "pointerOfReadonly.tag %s" %pointerOfReadonly.tag
print "accessing child of readonly"
tableInitiationPointer = pointerOfReadonly[0] // Here how to get the first child of readonly tag?
#print "accessing children of table"
#allRows = tableInitiationPointer.childNodes
Print "no of rows in the table = %s" %tableInitiationPointer.len("ROW_intf")// Returning none due to above non ability to find the tag
for row in tableInitiationPointer:
for subrows in row:
key = subrows.tag
value = subrows.value
dict[key]=value
No comments:
Post a Comment