The XML is stored in a remote file and I'll be using the requests library in Python to make an http request. If the request is successful, it will respond with the XML document (I'll get the XML content), and next "parse" the XML data (or content) using xml.etree.ElementTree module.
First you need to install the requests library. Because I am using VSCode, I can import requests module like this... from pip._vendor import requests.
Here's the example.
from pip._vendor import requests import xml.etree.ElementTree as ET def fetchLibrary (): try: response = requests.get("https://www.encodedna.com/library/library.xml") response.raise_for_status() # check if there are any http errors xmlData = response.content showTheList(xmlData ) # show xml data except requests.exceptions.RequestException as e: print(f"Failed to fetch data: {e}") def showTheList(xmlData ): try: root = ET.fromstring(xmlData ) book_list = root.findall('.//List') for book in book_list: book_name = book.find('BookName').text category = book.find('Category').text print(f"Book Name: {book_name}, Category: {category}") except ET.ParseError as e: print(f"Failed to parse XML data: {e}") fetchLibrary ()
Run the code and see the output in the "terminal" window.
xml.etree.ElementTree Module
The xml.etree.ElementTree module in Python, provides an API for parsing and creating XML data.
➡️ Learn more about xml.etree.ElementTree.
requests.get() Method
requests.get() method is part of the requests library that I am importing in the beginning of the example code.
The "requests.get()" method in Python, is used to send a GET request. The request is made using a URL. The URL is for the XML file, which is stored in a remote server.
ET.fromstring() Method
ET is the short for xml.etree.ElementTree. ET.fromstring() method parses the XML from a string into an element, which is the root element. You can check it like this.
root = ET.fromstring(xml_content) print(root)
Next, I'll find all the "<List>" tag inside XML. See this library file again
Finally, I'll loop through all the tags and get the details.