Skip to content

Instantly share code, notes, and snippets.

@guoqiao
Created September 14, 2016 21:36
Show Gist options
  • Save guoqiao/bd028f9492ea8b111acf08ccc5e3bc7e to your computer and use it in GitHub Desktop.
Save guoqiao/bd028f9492ea8b111acf08ccc5e3bc7e to your computer and use it in GitHub Desktop.
Extract element with namescape from xml by Python xml module
from xml.etree import ElementTree as ET
for prefix, uri in NAMESPACES.items():
ET.register_namespace(prefix, uri)
root_xml_element = ET.fromstring(xml)
token_xml_element = root_xml_element.find(
'soap:Body', namespaces=NAMESPACES
).find(
'wst:RequestSecurityTokenResponseCollection', namespaces=NAMESPACES
).find(
'wst:RequestSecurityTokenResponse', namespaces=NAMESPACES
).find(
'wst:RequestedSecurityToken', namespaces=NAMESPACES
).find(
'saml2:Assertion', namespaces=NAMESPACES
)
token_xml = ET.tostring(token_xml_element)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment