请帮助解析URL中的XML。 我正在使用'xml.etree.ElementTree'模块。 我的XML文档:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<systeminfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<machines>
<machine name="server1">
<applicationserver worker="server1:8173" status="Active"/>
<vizqlserver worker="server1:8349" status="Active"/>
<dataserver worker="server1:8723" status="Active"/>
</machine>
<machine name="server2">
<applicationserver worker="server2:8173" status="Active"/>
<vizqlserver worker="server2:8349" status="Active"/>
<dataserver worker="server2:8723" status="Active"/>
</machine>
</machines>
<service status="Active"/>
</systeminfo>
我需要为服务器1或服务器2进行otput:
applicationserver - Active
vizqlserver - Active
dataserver - Active
我的代码:
import requests
import xml.etree.ElementTree
req = requests.get("http://server/admin/systeminfo.xml")
systeminfo = ET.fromstring(req.content)
然后我不知道该写些什么。我阅读了文档https://docs.python.org/3/library/xml.etree.elementtree.html,尝试了不同的设计,但我做不到。机器名称将放在我的代码中。循环需要指定的计算机名称。
答案 0 :(得分:1)
我希望这就是你想要的:
for info in systeminfo.findall(".//machine[@name='server1']")[0]:
print(info.tag + ": " + info.attrib['status'])