从SQL Server读取HTTP XML响应:提取指定节点值求助
从SOAP XML响应中提取指定节点值
由于目标XML属于SOAP格式且带有命名空间,提取节点值时需要正确处理命名空间映射。以下是Python的实现示例:
假设HTTP响应的XML内容已存储在变量response_xml中:
import xml.etree.ElementTree as ET # 解析XML内容 root = ET.fromstring(response_xml) # 定义命名空间映射 namespaces = { 'soap': 'http://schemas.xmlsoap.org/soap/envelope/', 'ns': 'http://recharge/server' } # 提取RetCode节点值并去除多余空白字符 ret_code = root.find('.//ns:RetCode', namespaces).text.strip() # 提取TaskID节点值并去除多余空白字符 task_id = root.find('.//ns:TaskID', namespaces).text.strip() # 输出结果 print(f"RetCode: {ret_code}") print(f"TaskID: {task_id}")
执行代码后将得到:
- RetCode:
0 - TaskID:
U202207240001
内容的提问来源于stack exchange,提问作者Michael Marmah
相关产品推荐
相关产品推荐

