首页 > 解决方案 > 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)

标签: pythonxmlxml-namespaceselementtree

解决方案


YWeVDwuZwHdxxxxxxxxxxx_GqLtkNTE不在attrib。它是元素text

在这种情况下,属性是一个空字典

有关使用命名空间解析 XML dics 的信息,请参阅https://www.cmi.ac.in/~madhavan/courses/prog2-2012/docs/diveintopython3/xml.html 。


推荐阅读