With my below Python3 script, i am able to parse and convert XML records into lists , (by extracting value field out of it).
please help to improve this to print with name ":" value from the XML records.
for example: assuming below piece
<field name="RecordType" value="RESGJG"/>
<field name="RecordTypeHEC" value="PY"/>
getting output
RESGJG, PY
required output:
RecordType:RESGJG, RecordTypeHEC:PY
my input file: dummy.xml (##please note it has two records##every record starts with record source="AJS/SHD")
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record source="AJS/SHD" type="call">
<group name="General">
<field name="RecordType" value="RESGJG"/>
<field name="RecordTypeHEC" value="PY"/>
<field name="NodeID" value="rock.dsjjgds.cm"/>
<field name="SequenceNumber" value="7937973"/>
<field name="StartDate" value="20171049979"/>
<field name="EndDate" value="201704059739793"/>
<field name="CallDuration" value="973979i"/>
<field name="CauseForRecordClosing" value="normal"/>
</group>
<group name="SIP">
<field name="ICID" value="dshhkdhs"/>
<field name="CallID" value="[email protected]"/>
<field name="User-Agent" value="NotPresent"/>
<field name="Request-URI" value="sip:+47668384"/>
<field name="CalledPartyNumber" value="sip:+08779379972"/>
<field name="CallingPartyNumber" value="sip:[email protected]"/>
<field name="To" value="sip:+878379739"/>
<field name="From" value="sip:+937973962"/>
</group>
<group name="VPN">
<field name="VPN_NAME_B" value="blshahd"/>
<field name="VPN_Group_B" value="ctr"/>
<field name="B_ExtType" value="part"/>
<field name="B_ISDN" value="7973"/>
<field name="B_SIP" value="67367672"/>
<field name="B_PABXID" value="797397"/>
</group>
</record>
<record source="AJS/SHD" type="call">
<group name="General">
<field name="RecordType" value="MESGJG"/>
<field name="RecordTypeHEC" value="DY"/>
<field name="NodeID" value="rock.dsjjgds.cm"/>
<field name="SequenceNumber" value="7937973"/>
<field name="StartDate" value="20171049979"/>
<field name="EndDate" value="201704059739793"/>
<field name="CallDuration" value="973979i"/>
<field name="CauseForRecordClosing" value="normal"/>
</group>
<group name="SIP">
<field name="ICID" value="dshhkdhs"/>
<field name="CallID" value="[email protected]"/>
<field name="User-Agent" value="NotPresent"/>
<field name="Request-URI" value="sip:+47668384"/>
<field name="CalledPartyNumber" value="sip:+08779379972"/>
<field name="CallingPartyNumber" value="sip:[email protected]"/>
<field name="To" value="sip:+878379739"/>
<field name="From" value="sip:+937973962"/>
</group>
<group name="VPN">
<field name="VPN_NAME_B" value="blshahd"/>
<field name="VPN_Group_B" value="ctr"/>
<field name="B_ExtType" value="part"/>
<field name="B_ISDN" value="7973"/>
<field name="B_SIP" value="67367672"/>
<field name="B_PABXID" value="797397"/>
</group>
</record>
</records>
and i have already tried below script to parse XML fields and print in list format.
import sys
import operator
from functools import reduce
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("dummy.xml")
root = tree.getroot()
data = []
groups = root.findall('.//group')
for group in groups:
data.append([f.attrib['value'] for f in group.findall('./field')])
q = reduce(operator.concat, data)
s = ", ".join(q)
print(s)
getting Output as
RESGJG, PY, rock.dsjjgds.cm, 7937973, 20171049979, 201704059739793, 973979i, normal, dshhkdhs, [email protected], NotPresent, sip:+47668384, sip:+08779379972, sip:[email protected], sip:+878379739, sip:+937973962, blshahd, ctr, part, 7973, 67367672, 797397, MESGJG, DY, rock.dsjjgds.cm, 7937973, 20171049979, 201704059739793, 973979i, normal, dshhkdhs, [email protected], NotPresent, sip:+47668384, sip:+08779379972, sip:[email protected], sip:+878379739, sip:+937973962, blshahd, ctr, part, 7973, 67367672, 797397
required output:
RecordType:RESGJG, RecordTypeHEC:PY, NodeID:rock.dsjjgds.cm, SequenceNumber:7937973, StartDate:20171049979, EndDate:201704059739793, CallDuration:973979i, CauseForRecordClosing:normal, ICID:dshhkdhs, CallID:[email protected], User-Agent:NotPresent, Request-URI:sip:+47668384, CalledPartyNumber:sip:+08779379972, CallingPartyNumber:sip:[email protected], To:sip:+878379739, From:sip:+937973962, VPN_NAME_B:blshahd, VPN_Group_B:ctr, B_ExtType:part, B_ISDN:7973, B_SIP:67367672, B_PABXID:797397,
RecordType:MESGJG, RecordTypeHEC:DY, NodeID:rock.dsjjgds.cm, SequenceNumber:7937973, StartDate:20171049979, EndDate:201704059739793, CallDuration:973979i, CauseForRecordClosing:normal, ICID:dshhkdhs, CallID:[email protected], User-Agent:NotPresent, Request-URI:sip:+47668384, CalledPartyNumber:sip:+08779379972, CallingPartyNumber:sip:[email protected], To:sip:+878379739, From:sip:+937973962, VPN_NAME_B:blshahd, VPN_Group_B:ctr, B_ExtType:part, B_ISDN:7973, B_SIP:67367672, B_PABXID:797397,
Please help me
f.attrib['value']. You need to get alsof.attrib['name']... and makedataa dict because you wanted a dictionary.