You can import the library xml.etree.ElementTree
and with it to parse and read the XML data.
To parse, you can use the method fromstring
, to find a tag, use the method find
in the XML object, below is a very simple use example:
import xml.etree.ElementTree as ET
#Exemplo de XML qualquer
xml = '<?xml version="1.0" encoding="UTF-8"?>'
xml += ' <note>'
xml += ' <to>Alguém</to>'
xml += ' <from>De outra pessoa</from>'
xml += ' <heading>Recado</heading>'
xml += ' <body>Não esqueça...</body>'
xml += ' </note>'
#Efetuar o parser do XML
tree = ET.fromstring(xml)
#Exibe o conteúdo do elemento body
print(tree.find("body").text)
The method find
returns None
if you do not find the element.
See this online example: https://repl.it/repls/HungryPaltrySyndrome
Documentation: https://docs.python.org/3/library/xml.etree.elementtree.html
What I’m not getting is to take the call back to the Web Service and turn it into an XML. I haven’t found any way to make the return itself already come in XML format (according to your great example).
– Alex Souza
Can you call and return WS? The return is currently a string?
– Daniel Mendes
Yes, the return is a string. I confirm this using the type function that returns str.
– Alex Souza