繁体   English   中英

使用 lxml 解析 XML

[英]Parse XML using lxml

我有以下 XML 我需要解析。 我的大部分问题似乎是我无法让 StingIO 工作。 看起来我无法加载模块; 我想我什至不确定如何证明它已正确加载? 下面是 xml,作为对 http 请求的响应返回:

<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>

目标是将每个“current_call”的每个属性都放入它自己的变量中,这样我就可以将它们转储到其他地方的表中。 除非我可以将它们存储在 memory 或其他什么地方? 我真正想做的就是让它们再循环一个周期,或者直到我不再看到那个特定的“id”(我可以假设通话已经结束)。

我可以做类似的事情吗

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>

我相信有更好的方法来做到这一点!

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

给出:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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