2011-06-30 6 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」ごとに、それぞれの属性を取得することですので、私はテーブルにそれらをダンプすることができます他の場所。私はそれらをメモリなどに保存することができますか?私が本当にやりたいことは、もう1つのサイクルの間、またはその特定の「id」がもう見えなくなるまで(そして私はその呼び出しが終了したと仮定することができる)ことです。

は、私はこれを行うには良い方法をtheresの確信している

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'で試してみましたか?モジュールをロードできないということを意味しているかどうかは、標準のlibであるため、使用可能にする必要があります。どのようなエラーが出ますか? –

答えて

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

関連する問題