尝试解析Python中的XML响应(Trying to parse XML response in Python)

编程入门 行业动态 更新时间:2024-10-27 20:38:12
尝试解析Python中的XML响应(Trying to parse XML response in Python)

我试图从这个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 token

I 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'

更多推荐

本文发布于:2023-07-22 18:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222271.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XML   Python   response   parse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!