Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从URL解析Python中的XML_Python_Xml - Fatal编程技术网

从URL解析Python中的XML

从URL解析Python中的XML,python,xml,Python,Xml,请帮助解析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">

请帮助解析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>
我的代码:

import requests
import xml.etree.ElementTree

req = requests.get("http://server/admin/systeminfo.xml")
systeminfo = ET.fromstring(req.content)

然后我不知道写什么。我阅读了文档,尝试了不同的设计,但我做不到。机器名将输入我的代码中。循环中需要指定的机器名。

我希望这就是您想要的:

for info in systeminfo.findall(".//machine[@name='server1']")[0]:
    print(info.tag + ": " + info.attrib['status'])
for info in systeminfo.findall(".//machine[@name='server1']")[0]:
    print(info.tag + ": " + info.attrib['status'])