如何使用拆分函数或Python中更通用的方法将带有xml标记的字符串转换为字典?

我有一个字符串,实际上是一个大的xml结构字符串.

我想将此字符串转换为字典,以便我可以快速获取键值对.我怎么能这样做,这里的方式是什么?

我正在使用python3.

<TDS xmlns="http://uburw">
     <odour>
       <state>
         <order-number>AE:9E:7A:4E:8A:15</order-number>
        <oper-order-age>600</oper-order-age>
         <oper-order-time>200</oper-order-time>
        <oper-order-delay>1500</oper-order-delay>
         <branch-root>80008e5d3c4d1572</branch-root>
        <branch-root-path-cost>200000000</branch-root-path-cost>
         <branch-root-interface>20</branch-root-interface>
         <branch-root-if>
           <branch-base-name>ge2</branch-base-name>
           <branch-ext-id/>
         </branch-root-if>
        <time-since-top-change>191816</time-since-top-change>
         <function-changes>1</function-changes>
         <branch-root-hops>1</branch-root-hops>
       </state>
   <leaves>
        <leaf>
          <leaf-name>ge1</leaf-name>
         </leaf>
        <leaves>

最佳答案
xmltodict这样做很容易.

例:

xmltodict.parse("""
<?xml version="1.0" ?>
<person>
  <name>john</name>
  <age>20</age>
</person>""")

输出:

{u'person': {u'age': u'20', u'name': u'john'}}
点赞