[英]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->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>
目標是將每個“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->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)
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.