递归解析XML

2024-06-16 12:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在使用Recurly Python库(https://github.com/recurly/recurly-client-python),但似乎找不到一种简单的方法来解析来自API的XML响应。有人看到这个库中专门用于解析XML字符串的部分吗?我担心构建自定义解析器,然后XML格式会发生变化

我做了一个简单的http请求请求事务,我得到了一个XML字符串,就像下面粘贴的一样,但我在他们的库中没有看到任何东西可以将其转换为某种类型的解析对象,我可以轻松地将其转换为数据框

  <account href="https://your-subdomain.recurly.com/v2/accounts/1"/>
  <invoice href="https://your-subdomain.recurly.com/v2/invoices/1006"/>
  <subscriptions href="https://your-subdomain.recurly.com/v2/transactions/37bff9ddc1e468a59f6fef417cb32281/subscriptions"/>
  <original_transaction href="https://your-subdomain.recurly.com/v2/transactions/37bfef7b3873aed2af2a3d4d35878e5f"/>
  <uuid>37bff9ddc1e468a59f6fef417cb32281</uuid>
  <action>refund</action>
  <amount_in_cents type="integer">870</amount_in_cents>
  <tax_in_cents type="integer">70</tax_in_cents>
  <currency>EUR</currency>
  <status>success</status>
  <payment_method>credit_card</payment_method>
  <reference>1171163</reference>
  <source>subscription</source>
  <recurring type="boolean">false</recurring>
  <test type="boolean">true</test>
  <voidable type="boolean">true</voidable>
  <refundable type="boolean">false</refundable>
  <ip_address nil="nil"/>
  <cvv_result code="" nil="nil"/>
  <avs_result code="D">Street address and postal code match.</avs_result>
  <avs_result_street nil="nil"/>
  <avs_result_postal nil="nil"/>
  <created_at type="datetime">2016-08-03T16:13:44Z</created_at>
  <updated_at type="datetime">2016-08-03T16:13:44Z</updated_at>
  <details>
    <account>
      <account_code>1</account_code>
      <first_name>Verena</first_name>
      <last_name>Example</last_name>
      <company>Recurly Inc</company>
      <email>verena@example.com</email>
      <billing_info type="credit_card">
        <first_name>Verena</first_name>
        <last_name>Example</last_name>
        <address1>123 Main St.</address1>
        <address2 nil="nil"/>
        <city>San Francisco</city>
        <state>CA</state>
        <zip>94105</zip>
        <country>US</country>
        <phone nil="nil"/>
        <vat_number nil="nil"/>
        <card_type>Visa</card_type>
        <year type="integer">2019</year>
        <month type="integer">12</month>
        <first_six>411111</first_six>
        <last_four>1111</last_four>
      </billing_info>
    </account>
  </details>
  <a name="refund" href="https://your-subdomain.recurly.com/v2/transactions/37bff9ddc1e468a59f6fef417cb32281" method="delete"/>
</transaction>

Tags: subdomainnamehttpscomyourtypecodeaccount