2011-06-30 110 views
1

我有以下需要解析的XML。我的問題大部分似乎是我無法讓StingIO工作。看起來我無法加載模塊;我想我甚至不知道如何顯示它已正確加載?下面是XML,返回一個HTTP請求的響應:使用lxml解析XML

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

我敢肯定,那裏有一個更好的方式來做到這一點!

+0

什麼碼你嘗試過用'StringIO'和/或'lxml'?不確定你無法加載模塊是什麼意思,應該是可用的,因爲它是標準庫。你會得到什麼錯誤? –

回答

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

太棒了。我很親密,但這正是我所追求的!非常感謝。 – lorsungcu

+0

不客氣! – Acorn