在Python中获取SOAP响应的结果标记之间的所有内容
我有这样一个SOAP响应:在Python中获取SOAP响应的结果标记之间的所有内容,python,xml,soap,Python,Xml,Soap,我有这样一个SOAP响应: <?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>
<?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>
<GetCurrencyCodeByCurrencyNameResponse xmlns="http://www.webserviceX.NET">
<GetCurrencyCodeByCurrencyNameResult>
<NewDataSet />
</GetCurrencyCodeByCurrencyNameResult>
</GetCurrencyCodeByCurrencyNameResponse>
</soap:Body></soap:Envelope>
<?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>
<GetUserInfoResponse xmlns="http://tempuri.org/">
<GetUserInfoResult>
<ErrorOccured>true</ErrorOccured>
<ErrorStr>System.Data.OleDb.OleDbException: Conversion failed when converting the varchar value '4CuTrO8O6Tn' to data type int.
at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
at System.Data.OleDb.OleDbDataReader.NextResult()
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at Service.GetUserInfo(String username, String password)
</ErrorStr>
<SqlQuery>SELECT * FROM users WHERE username=''+(select convert(int,CHAR(52)+CHAR(67)+CHAR(117)+CHAR(84)+CHAR(114)+CHAR(79)+CHAR(56)+CHAR(79)+CHAR(54)+CHAR(84)+CHAR(110)) FROM syscolumns)+'' AND password='32cc5886dc1fa8c106a02056292c4654'
</SqlQuery><id>-1</id><joindate>0001-01-01T00:00:00</joindate>
</GetUserInfoResult>
</GetUserInfoResponse>
</soap:Body></soap:Envelope>
但是,当我有一个响应,其中在results标记中包含其他标记时,其他子级(如SOAP响应):
<?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>
<GetCurrencyCodeByCurrencyNameResponse xmlns="http://www.webserviceX.NET">
<GetCurrencyCodeByCurrencyNameResult>
<NewDataSet />
</GetCurrencyCodeByCurrencyNameResult>
</GetCurrencyCodeByCurrencyNameResponse>
</soap:Body></soap:Envelope>
<?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>
<GetUserInfoResponse xmlns="http://tempuri.org/">
<GetUserInfoResult>
<ErrorOccured>true</ErrorOccured>
<ErrorStr>System.Data.OleDb.OleDbException: Conversion failed when converting the varchar value '4CuTrO8O6Tn' to data type int.
at System.Data.OleDb.OleDbDataReader.ProcessResults(OleDbHResult hr)
at System.Data.OleDb.OleDbDataReader.NextResult()
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
at Service.GetUserInfo(String username, String password)
</ErrorStr>
<SqlQuery>SELECT * FROM users WHERE username=''+(select convert(int,CHAR(52)+CHAR(67)+CHAR(117)+CHAR(84)+CHAR(114)+CHAR(79)+CHAR(56)+CHAR(79)+CHAR(54)+CHAR(84)+CHAR(110)) FROM syscolumns)+'' AND password='32cc5886dc1fa8c106a02056292c4654'
</SqlQuery><id>-1</id><joindate>0001-01-01T00:00:00</joindate>
</GetUserInfoResult>
</GetUserInfoResponse>
</soap:Body></soap:Envelope>
我无法使用前面的代码获取结果标记之间的内容。
那么,如何在SOAP响应的结果标记之间获取全部内容呢?我不太清楚您到底想要什么,但这可能会做到:
# This gets all of the text data in the indicated region
import xml.etree.ElementTree as ET
root = ET.fromstring(SoapResponse)
child=root[0][0][0]
contenu = ET.tostring(child, encoding='UTF-8', method='text').decode('UTF-8')
或
1您将结果标签称为什么?我没有看到任何名为result的标记。2您希望整个内容以什么形式呈现?XML片段?ET元素?SOAP响应的结果标记是根[0][0][0]命名的。因为我有很多SOAP响应要分析,所以我没有所有方法的名称,所以我使用根[0][0][0]来获取我需要的元素。对于表单,我需要一个XML片段或一个stringUsing contenu=ET.tostringchild,编码='UTF-8',method='text'。解码'UTF-8'我只得到文本,但我想得到和所有XML片段之间的确切内容。在这种情况下,您需要相同的调用,但使用的是方法='XML'。请参阅我的编辑。当然,如果你打算对结果做进一步的分析,使用child可能更容易。