简体   繁体   中英

Parse XML using lxml

I have the following XML I need to parse. Much of my issue seems to be that I can't get StingIO to work. It looks like I can't load the module; I guess I'm not even sure how to show that it's loaded properly? Below is the xml, returned as a response to an 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-&gt;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-&gt;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-&gt;ulaw" />
            </current_calls>
    </result>

The goal is to get each attribute, per 'current_call' into it's own variable, so I can dump them into a table elsewhere. Unless I can store them in memory or something? All I really want to do is keep them for one more cycle, or until I do not see that particular 'id' anymore (and I can assume the call has ended).

Can I do something like

for root.result.current_calls.current_call in root.result.current_calls:
        id = root.result.current_calls.current_call.get("id")
        .
        .
        <send variables to database within for.. loop>

I'm sure theres a better way to do 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-&gt;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-&gt;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-&gt;ulaw" />
            </current_calls>
    </result>
</response>
"""

tree = etree.fromstring(xml_string)

for 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'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM