Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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
在python中读取xml 真的 AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA_Python_Xml - Fatal编程技术网

在python中读取xml 真的 AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA

在python中读取xml 真的 AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA,python,xml,Python,Xml,这个xml格式来自sope api,我想从这个表单中读取xml aSessionID。请帮助我在python中执行此操作list\u test.xml: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x

这个xml格式来自sope api,我想从这个表单中读取xml aSessionID。请帮助我在python中执行此操作

list\u test.xml:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <LoginResponse xmlns="http://tempuri.org/">
            <LoginResult>true</LoginResult>
            <aSessionID>AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA</aSessionID>
        </LoginResponse>
    </soap:Body>
</soap:Envelope>
输出

from xml.dom import minidom

doc = minidom.parse("list_test.xml")
sessionList = doc.getElementsByTagName('aSessionID')

for sess in sessionList:
    print(sess.firstChild.nodeValue)
from xml.dom import minidom

xml_str = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <LoginResponse xmlns="http://tempuri.org/">
            <LoginResult>true</LoginResult>
            <aSessionID>AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA</aSessionID>
            <aSessionID>54F-6A-51-FD-E6-8D-C8-45-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-65</aSessionID>
        </LoginResponse>
    </soap:Body>
</soap:Envelope>'''
doc = minidom.parseString(xml_str)
sessionList = doc.getElementsByTagName('aSessionID')

for sess in sessionList:
    print(sess.firstChild.nodeValue)
编辑

from xml.dom import minidom

doc = minidom.parse("list_test.xml")
sessionList = doc.getElementsByTagName('aSessionID')

for sess in sessionList:
    print(sess.firstChild.nodeValue)
from xml.dom import minidom

xml_str = '''<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <LoginResponse xmlns="http://tempuri.org/">
            <LoginResult>true</LoginResult>
            <aSessionID>AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA</aSessionID>
            <aSessionID>54F-6A-51-FD-E6-8D-C8-45-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-65</aSessionID>
        </LoginResponse>
    </soap:Body>
</soap:Envelope>'''
doc = minidom.parseString(xml_str)
sessionList = doc.getElementsByTagName('aSessionID')

for sess in sessionList:
    print(sess.firstChild.nodeValue)
要从
字符串而不是文件中读取
xml
,可以使用:

AF-6A-51-FD-E6-8D-C8-12-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-CA
54F-6A-51-FD-E6-8D-C8-45-AB-7E-C1-BD-50-7A-43-D0-AA-27-15-65
因此:

minidom.parseString(xml_str)

看看下面的答案是否有帮助?如果这个xml不在文件中,它是字符串,那么我怎么做?