首页 > 解决方案 > OSError: [Errno 22] Invalid argument 在 python 中解析 xml 时获取无效参数

问题描述

我在下面的 xml 中尝试提取所需的值


xml = '<s:Envelope xmlns:s="schemas.xmlsoap.org/soap/envelope/"><s:Body><GetService xmlns="abc.com/Service">
<GetService xmlns:a="abc.com" xmlns:i="www.w3.org/2001/XMLSchema-instance">
<a:EnvironmentName>test</a:EnvironmentName><a:HasUpdates>true</a:HasNewUpdates><a:active>false</a:active><a:Time>13:37:22</a:Time><a:ServiceUrisString>&lt;s&gt;&lt;u t="1" n="net://abc.com/" i="net.tcp://abc.com/" /&gt;&lt;u t="2"
" /&gt;&lt;/s&gt;</a:ServiceUrisString></GetService></GetServiceRegistryResponse></s:Body></s:Envelope>
'

我正在寻找的是从上面的 xml 中获取“活动”和“时间”的值

active=false
time=13:37:22

下面是代码

import xml.etree.ElementTree as ET
tree=ET.parse(xml)
print(tree)

但是解析没有按预期工作下面我收到以下错误

OSError:[Errno 22] 无效参数:

那么如何解决这个问题?

标签: python-3.xxml

解决方案


ET.parse() 是一个从磁盘读取 xml 的函数

尝试 ET.fromstring(xml)


推荐阅读