2011-06-30 191 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