我试图从这个XML中获得2个元素( Result和AuthenticationKey )的值。 <s:Envelope>语法将我抛弃。
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <AuthenticateResponse xmlns="http://remote.Services"> <AuthenticateResult xmlns:a="http://schemas.datacontract.org/2004/07/remote.Services.Api.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <AdditionalInfo i:nil="true"/> <ErrorMessage i:nil="true"/> <ErrorMessageId i:nil="true"/> <ErrorMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <InternalId>0</InternalId> <RecordsAffected>0</RecordsAffected> <Result>true</Result> <WarningMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <a:AuthenticationKey>SUPERSECRETTOKEN</a:AuthenticationKey> <a:UserGrpId>YYY</a:UserGrpId> <a:UserId>XXX</a:UserId> </AuthenticateResult> </AuthenticateResponse> </s:Body> </s:Envelope>这是返回None的相关代码行。 两行代码都返回一个None。
我试图使用来自stackoverflow的另一个问题的代码,但我不知道如何在这个特定的XML的find方法中引入命名空间。
tree = ElementTree.fromstring(resp.text) token = tree.find("AuthenticateResponse") #Option1 token = tree.find("Envelope/Body/AuthenticateResponse/AuthenticateResult/AuthenticationKey") #Option2 print tokenI am trying to obtain the value of 2 elements (Result and AuthenticationKey)from this XML. The <s:Envelope> syntax is throwing me off.
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <AuthenticateResponse xmlns="http://remote.Services"> <AuthenticateResult xmlns:a="http://schemas.datacontract.org/2004/07/remote.Services.Api.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <AdditionalInfo i:nil="true"/> <ErrorMessage i:nil="true"/> <ErrorMessageId i:nil="true"/> <ErrorMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <InternalId>0</InternalId> <RecordsAffected>0</RecordsAffected> <Result>true</Result> <WarningMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <a:AuthenticationKey>SUPERSECRETTOKEN</a:AuthenticationKey> <a:UserGrpId>YYY</a:UserGrpId> <a:UserId>XXX</a:UserId> </AuthenticateResult> </AuthenticateResponse> </s:Body> </s:Envelope>Here is the relevant line of code that is returning a None. Both lines of code are returning a None.
I tried to use the code from another question on stackoverflow, but I am not sure on how to introduce the namespaces in the find method for this particular xml.
tree = ElementTree.fromstring(resp.text) token = tree.find("AuthenticateResponse") #Option1 token = tree.find("Envelope/Body/AuthenticateResponse/AuthenticateResult/AuthenticationKey") #Option2 print token最满意答案
xmlns部分成为您的tags的前缀,即找到您需要搜索{http://remote.Services}AuthenticateResponse 。
print(list(tree)[0].find('{http://remote.Services}AuthenticateResponse')) >>> <Element '{http://remote.Services}AuthenticateResponse' at 0x00000000027228B8>对于更通用的解决方案,请在此查看答案。
from io import StringIO tree = ET.iterparse(StringIO("""<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <AuthenticateResponse xmlns="http://remote.Services"> <AuthenticateResult xmlns:a="http://schemas.datacontract.org/2004/07/remote.Services.Api.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <AdditionalInfo i:nil="true"/> <ErrorMessage i:nil="true"/> <ErrorMessageId i:nil="true"/> <ErrorMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <InternalId>0</InternalId> <RecordsAffected>0</RecordsAffected> <Result>true</Result> <WarningMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <a:AuthenticationKey>SUPERSECRETTOKEN</a:AuthenticationKey> <a:UserGrpId>YYY</a:UserGrpId> <a:UserId>XXX</a:UserId> </AuthenticateResult> </AuthenticateResponse> </s:Body> </s:Envelope>""")) for _, element in tree: element.tag = element.tag.split('}')[-1] print(tree.root.find('Body').find('AuthenticateResponse').find('AuthenticateResult').find('AuthenticationKey').text) >>> 'SUPERSECRETTOKEN'The xmlns part becomes a prefix for your tags, i.e. to find AuthenticateResponse you would need to search for {http://remote.Services}AuthenticateResponse.
print(list(tree)[0].find('{http://remote.Services}AuthenticateResponse')) >>> <Element '{http://remote.Services}AuthenticateResponse' at 0x00000000027228B8>For a more general solution have a look at the answer here.
from io import StringIO tree = ET.iterparse(StringIO("""<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <AuthenticateResponse xmlns="http://remote.Services"> <AuthenticateResult xmlns:a="http://schemas.datacontract.org/2004/07/remote.Services.Api.DataContracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <AdditionalInfo i:nil="true"/> <ErrorMessage i:nil="true"/> <ErrorMessageId i:nil="true"/> <ErrorMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <InternalId>0</InternalId> <RecordsAffected>0</RecordsAffected> <Result>true</Result> <WarningMessages i:nil="true" xmlns:b="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/> <a:AuthenticationKey>SUPERSECRETTOKEN</a:AuthenticationKey> <a:UserGrpId>YYY</a:UserGrpId> <a:UserId>XXX</a:UserId> </AuthenticateResult> </AuthenticateResponse> </s:Body> </s:Envelope>""")) for _, element in tree: element.tag = element.tag.split('}')[-1] print(tree.root.find('Body').find('AuthenticateResponse').find('AuthenticateResult').find('AuthenticationKey').text) >>> 'SUPERSECRETTOKEN'更多推荐
发布评论