python - python解析xml获取值
问题描述
我有一个来自我的系统的 xml 响应,我试图使用 python 代码获取值。需要专家观点突出我的错误。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns3:loginResponse xmlns:ns2="http://ws.core.product.xxxxx.com/groupService/" xmlns:ns3="http://ws.core.product.xxxxx.com/loginService/" xmlns:ns4="http://ws.core.product.xxxxx.com/userService/"><ns3:return>YWeVDwuZwHdxxxxxxxxxxx_GqLtkNTE.</ns3:return></ns3:loginResponse>
我正在使用下面的代码代码并且没有运气获得价值 - YWeVDwuZwHdxxxxxxxxxxx_GqLtkNTE
。我没有使用带命名空间的 xml 解析。response.text 具有上述 xml 响应。
responsetree = ET.ElementTree(ET.fromstring(response.text))
responseroot = responsetree.getroot()
for a in root.iter('return'):
print(a.attrib)
解决方案
YWeVDwuZwHdxxxxxxxxxxx_GqLtkNTE
不在attrib
。它是元素text
在这种情况下,属性是一个空字典
有关使用命名空间解析 XML dics 的信息,请参阅https://www.cmi.ac.in/~madhavan/courses/prog2-2012/docs/diveintopython3/xml.html 。
推荐阅读
- reactjs - 用户在移动浏览器和页面之间切换屏幕后,在 React Native 中重新渲染生命周期
- python - pandas groupby 是否通过引用或值传递?
- json - 接受 POST WebHook 的 Vb.net 桌面应用程序
- javascript - 带有角度指令 ngIf 的 Animejs
- python - 使用包含路径的选项卡对图像进行分类
- php - 写入 CSV 文件时 PHP 变量为空
- mysql - Docker 容器无法连接到它的 mysql 服务器(访问被拒绝)
- android - 无法从 Firebase 获取 RemoteConfig 数据
- expo - Expo:如何以编程方式更改启动图像?
- vue.js - 在 Vuetify Vue 中添加自定义图标或徽标