在 Python 中使用 xmltodict 删除命名空间 [英] Remove namespace with xmltodict in Python

查看:66
本文介绍了在 Python 中使用 xmltodict 删除命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xmltodict 将 XML 转换为 Python 字典.它支持命名空间.我可以按照主页上的示例并成功删除命名空间.但是,我无法从 XML 中删除命名空间并且无法确定原因?这是我的 XML:

xmltodict converts XML to a Python dictionary. It supports namespaces. I can follow the example on the homepage and successfully remove a namespace. However, I cannot remove the namespace from my XML and cannot identify why? Here is my XML:

<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
<section1
    mystatus:field1="data1"
    mystatus:field2="data2" />
<section2
    mystatus:lineA="outputA"
    mystatus:lineB="outputB" />
</status>

并使用:

xmltodict.parse(xml,process_namespaces=True,namespaces={'http://localhost/mystatus':None})

我明白了:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@http://localhost/mystatus:field1', u'data1'), (u'@http://localhost/mystatus:field2', u'data2')])), (u'section2', OrderedDict([(u'@http://localhost/mystatus:lineA', u'outputA'), (u'@http://localhost/mystatus:lineB', u'outputB')]))]))])

代替:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'field1', u'data1'), (u'field2', u'data2')])), (u'section2', OrderedDict([(u'lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])

我是不是犯了一些简单的错误,或者我的 XML 是否有什么地方阻止了 process_namespace 修改正常工作?

Am I making some simple mistake, or is there something about my XML that prevents the process_namespace modification from working correctly?

推荐答案

xmltodict 基于 expat,所以命名空间应该应用于类名,而不是属性名:

xmltodict is based on expat, so namespaces should applied to the class name, not attribute names:

<?xml version="1.0" encoding="UTF-8"?>
<status xmlns:mystatus="http://localhost/mystatus">
    <mystatus:section1 field1="data1" field2="data2" />
    <mystatus:section2 lineA="outputA" lineB="outputB" />
</status>

解析时:

foo = xmltodict.parse(xml,
                      process_namespaces=True,
                      namespaces={'http://localhost/mystatus':None})

输出:

OrderedDict([(u'status', OrderedDict([(u'section1', OrderedDict([(u'@field1', u'data1'), (u'@field2', u'data2')])), (u'section2', OrderedDict([(u'@lineA', u'outputA'), (u'@lineB', u'outputB')]))]))])

访问它很容易:

# Get attribute 'lineA' from class 'section2' from class 'status'
>>> foo.get('status').get('section2').get('@lineA')
u'outputA'

属性命名空间仅当您有多个同名属性时才需要(例如多个 id 或多个价格等),在这种情况下,我无法获得 expatxmltodict 正确解析它.虽然是 YMMV.

Attribute namespaces are only required when you have multiple attributes of the same name (e.g. multiple id's or multiple prices, etc), in which case, I couldn't get expat or xmltodict to parse it correctly. YMMV though.

这篇关于在 Python 中使用 xmltodict 删除命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆