0

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

1
  • You only get f.attrib['value']. You need to get also f.attrib['name']... and make data a dict because you wanted a dictionary. Commented Nov 14, 2019 at 11:48

1 Answer 1

2

Your code only fetches the value attribute, it completly ignores the name.

Also, using reduce is somewhat an overkill.

groups = root.findall('.//group')
for group in groups:
    print(', '.join('{}: {}'.format(field.attrib['name'], field.attrib['value']) for field in group.findall('./field')))
    print()

Will 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
Sign up to request clarification or add additional context in comments.

5 Comments

Hello.. thanks for reply.. i am new to programming, and not very sure about mentioning "dict format" is proper way to communicate or not.. But my required output is same as mentioned above.
Your first code is fine,only problem i have is that i need to print all fields under dict {}, separately per record/ means this dummy.xml has 2 records.. may i get help to print them line by line separately. (like i mentioned in required output)
@SSK please decide the required output. It started from a dict, went to a string now it's a dict again?
Sorry.. i didnt see updated answer before i sent my comment.. but thanks.. it helps and fulfilled my requirement.
THANKS A LOT.. i got required output from updated code above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.