xml - 具有多个命名空间的 XML 解析
问题描述
我正在尝试使用 Python 的用于 xml 解析的标准库来解析和编写 XML。
xml的格式如下:
xml = '<class:Classification xmlns:class="schema:SpeciesClassification:2.0" xmlns="http://www.w3.org/1999/xhtml" dateClassified="2019-02-11" endangeredMarking="false" caveat="false"></class:Classification>'
当我解析这个 xml 然后将它推回一个字符串时,我得到了一些奇怪的东西。都class:
变成了ns0
。如何保留这些标记?
from xml.etree.cElementTree import Element, SubElement, parse, tostring, fromstring
print(tostring(fromstring(xml)))
b'<ns0:Classification xmlns:ns0="schema:SpeciesClassification:2.0" caveat="false" dateClassified="2019-02-11" endangeredMarking="false" />'
我必须指定不同的解析器吗?我只是有点迷失为什么这会被丢弃。
谢谢
解决方案
推荐阅读
- python-3.x - 生成 1 到 1000 之间的随机整数
- javascript - 当我使用 Excel JavaScript API 构建加载项时,Microsoft.Office.WebExtension 不起作用
- hadoop - 如何在 Hive 中获取表属性的默认值?
- postgresql - 如何解决 pg_dump:由于服务器版本不匹配而中止?
- google-cloud-functions - 在对话流中处理 NO USER INPUT
- c++ - 在最初定义在内联命名空间的封闭命名空间中的内联命名空间中扩展命名空间
- java - CodeCov 找不到报告
- java - 正则表达式从字符串中检索最后一位数字并对它们进行排序
- r - 自定义 DataTables 如何在 Shiny 中显示缺失值
- c# - 将方法作为参数传递而不使用委托