在 xmltodict 中强制使用 `#text` 属性

问题描述

解析没有任何属性的 XML 文档时,例如

<Parent>
  <Child>3</Child>
</Parent>

xmltodict 将让您无需通过 OrderedDict 即可直接访问该值:

>>> xmltodict.parse(xml)
OrderedDict([('Parent',OrderedDict([('Child','3')]))])

但如果是这样:

<Parent>
  <Child type="human">3</Child>
</Parent>

然后我得到:

OrderedDict([('Parent',OrderedDict([('@type','human'),('#text','3')]))]))])

如何强制 xmltodict 始终将内部文本作为 #text 属性返回,因为有时我的 XML 文档可能缺少 XML 属性 type='human'

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)