使用lxml解析XML

时间:2011-06-30 21:56:59

标签: python xml iteration 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'将每个属性放入其自己的变量中,这样我就可以将它们转储到其他地方的表中。除非我可以将它们存储在内存或其他东西中?我真正想做的就是让它们再保持一个循环,或者直到我再也看不到那个特定的“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>

我确信这是一个更好的方法!

1 个答案:

答案 0 :(得分:4)

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