python - Parse XML using lxml -
i have following xml need parse. of issue seems can't stingio work. looks can't load module; guess i'm not sure how show it's loaded properly? below xml, returned response http request:
<response method="switchvox.currentcalls.getlist"> <result> <current_calls total_items="3"> <current_call id="sip/6525-b59313c8" from_caller_id_name="user1" from_caller_id_number="user1_ext" to_caller_id_name="callee1" to_caller_id_number="callee1_num" start_time="2011-06-30 15:44:17" duration="346" state="talking" provider="internal" format="g722->g722" /> <current_call id="sip/4476-b595a0a0" from_caller_id_name="user2" from_caller_id_number="user1_ext" to_caller_id_name="callee2" to_caller_id_number="callee2_num" start_time="2011-06-30 15:48:44" duration="79" state="talking" provider="vcg_b" format="g722->ulaw" /> <current_call id="sip/4483-0aa41320" from_caller_id_name="user3" from_caller_id_number="user1_ext" to_caller_id_name="callee3" to_caller_id_number="callee3_num" start_time="2011-06-30 15:47:54" duration="129" state="talking" provider="vcg_b" format="g722->ulaw" /> </current_calls> </result>
the goal each attribute, per 'current_call' it's own variable, can dump them table elsewhere. unless can store them in memory or something? want keep them 1 more cycle, or until not see particular 'id' anymore (and can assume call has ended).
can like
for root.result.current_calls.current_call in root.result.current_calls: id = root.result.current_calls.current_call.get("id") . . <send variables database within for.. loop>
i'm sure theres better way this!
from lxml import etree xml_string = """ <response method="switchvox.currentcalls.getlist"> <result> <current_calls total_items="3"> <current_call id="sip/6525-b59313c8" from_caller_id_name="user1" from_caller_id_number="user1_ext" to_caller_id_name="callee1" to_caller_id_number="callee1_num" start_time="2011-06-30 15:44:17" duration="346" state="talking" provider="internal" format="g722->g722" /> <current_call id="sip/4476-b595a0a0" from_caller_id_name="user2" from_caller_id_number="user1_ext" to_caller_id_name="callee2" to_caller_id_number="callee2_num" start_time="2011-06-30 15:48:44" duration="79" state="talking" provider="vcg_b" format="g722->ulaw" /> <current_call id="sip/4483-0aa41320" from_caller_id_name="user3" from_caller_id_number="user1_ext" to_caller_id_name="callee3" to_caller_id_number="callee3_num" start_time="2011-06-30 15:47:54" duration="129" state="talking" provider="vcg_b" format="g722->ulaw" /> </current_calls> </result> </response> """ tree = etree.fromstring(xml_string) call in tree.xpath('.//current_call'): print call.attrib
gives:
{'from_caller_id_number': 'user1_ext', 'to_caller_id_name': 'callee1', 'start_time': '2011-06-30 15:44:17', 'format': 'g722->g722', 'to_caller_id_number': 'callee1_num', state': 'talking', 'provider': 'internal', 'duration': '346', 'id': 'sip/6525-b59313c8', 'from_caller_id_name': 'user1'} {'from_caller_id_number': 'user1_ext', 'to_caller_id_name': 'callee2', 'start_time': '2011-06-30 15:48:44', 'format': 'g722->ulaw', 'to_caller_id_number': 'callee2_num', state': 'talking', 'provider': 'vcg_b', 'duration': '79', 'id': 'sip/4476-b595a0a0', 'from_caller_id_name': 'user2'} {'from_caller_id_number': 'user1_ext', 'to_caller_id_name': 'callee3', 'start_time': '2011-06-30 15:47:54', 'format': 'g722->ulaw', 'to_caller_id_number': 'callee3_num', state': 'talking', 'provider': 'vcg_b', 'duration': '129', 'id': 'sip/4483-0aa41320', 'from_caller_id_name': 'user3'}
Comments
Post a Comment