Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pytest = "^7.1.3"
requests = "^2.28.1"
jupyter = "^1.0.0"
ipython = "^8.5.0"
defusedxml = "==0.7.1"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package is recommended by the Python community to protect against XML vulnerabilities.

License: PSF-2.0Open SourceMore facts



[tool.poetry.group.dev.dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# print(dir(xml))

import xml.etree.ElementTree as ET
from xml.dom import minidom
import defusedxml.minidom

# print(dir(ET))

Expand All @@ -42,7 +42,7 @@
print()


result_str2 = minidom.parseString(
result_str2 = defusedxml.minidom.parseString(
ET.tostring(root)
).toprettyxml()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""
Purpose: Reading(Parsing) XML
"""

import xml.etree.ElementTree as ET
from pprint import pp
import defusedxml.ElementTree

tree = ET.parse("books.xml")
tree = defusedxml.ElementTree.parse("books.xml")

# print(dir(tree))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"""
Purpose: To parse(read) xml string
"""

import xml.etree.ElementTree as ET
import defusedxml.ElementTree

input_string = """
<stuff>
Expand All @@ -18,7 +17,7 @@
</users>
</stuff>"""

stuff_tree = ET.fromstring(input_string)
stuff_tree = defusedxml.ElementTree.fromstring(input_string)

nodes = stuff_tree.findall("users") # child level
print(nodes)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/python3

import xml.etree.ElementTree as ElementTree
import defusedxml.ElementTree

data = """
<person>
Expand All @@ -11,6 +11,6 @@
<email hide="yes"/>
</person>"""

tree = ElementTree.fromstring(data)
tree = defusedxml.ElementTree.fromstring(data)
print("Name:", tree.find("name").text)
print("Attr:", tree.find("email").get("hide"))
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import xml.etree.ElementTree as et

import requests
import defusedxml.ElementTree


# Exercise 5.1
Expand All @@ -14,7 +15,7 @@ def test_check_root_of_xml_response():
response = requests.get(
"http://parabank.parasoft.com/parabank/services/bank/accounts/12345", timeout=60
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
root = xml_tree.getroot()
assert root.tag == "account"
Expand All @@ -32,7 +33,7 @@ def test_check_specific_element_of_xml_response():
response = requests.get(
"http://parabank.parasoft.com/parabank/services/bank/accounts/12345", timeout=60
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
first_name = xml_tree.find("customerId")
assert first_name.text == "12212"
Expand All @@ -49,7 +50,7 @@ def test_check_number_of_accounts_for_12212_greater_than_five():
"http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts",
timeout=60,
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
accounts = xml_tree.findall(".//account")
assert len(accounts) > 5
Expand All @@ -66,7 +67,7 @@ def test_use_xpath_for_more_sophisticated_checks():
"http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts",
timeout=60,
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
savings_accounts = xml_tree.findall(".//account/type[.='SAVINGS']")
assert len(savings_accounts) > 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import xml.etree.ElementTree as et

import requests
import defusedxml.ElementTree


def test_check_root_of_xml_response():
response = requests.get(
"http://parabank.parasoft.com/parabank/services/bank/customers/12212",
timeout=60,
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
root = xml_tree.getroot()
assert root.tag == "customer"
Expand All @@ -20,7 +21,7 @@ def test_check_specific_element_of_xml_response():
"http://parabank.parasoft.com/parabank/services/bank/customers/12212",
timeout=60,
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
first_name = xml_tree.find("firstName")
assert first_name.text == "John"
Expand All @@ -33,7 +34,7 @@ def test_use_xpath_for_more_sophisticated_checks():
"http://parabank.parasoft.com/parabank/services/bank/customers/12212",
timeout=60,
)
response_body_as_xml = et.fromstring(response.content)
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
xml_tree = et.ElementTree(response_body_as_xml)
address_children = xml_tree.findall(".//address/*")
assert len(address_children) == 4
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from numbers import Number
from typing import Optional
from xml.dom.minidom import parseString

import requests
import defusedxml.minidom

SETTINGS = {
"currency": "THB",
Expand All @@ -19,7 +19,7 @@ def check_exchange_rate(
res = requests.get(URL, timeout=60)

# we have to parse XML (unfortunately I did not find a .json API)
parsed = parseString(
parsed = defusedxml.minidom.parseString(
str(res.content.decode("utf-8")).replace("\n", "").replace("\t", "")
)
currency_rates = parsed.childNodes[0].childNodes[2].childNodes[0].childNodes
Expand Down
4 changes: 2 additions & 2 deletions python3/16_Web_Services/h_feedparsing/parse_RSS_feed.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Python code to illustrate parsing of XML files
# importing the required modules
import csv
import xml.etree.ElementTree as ET

import requests
import defusedxml.ElementTree


def loadRSS():
Expand All @@ -20,7 +20,7 @@ def loadRSS():

def parseXML(xmlfile):
# create element tree object
tree = ET.parse(xmlfile)
tree = defusedxml.ElementTree.parse(xmlfile)

# get root element
root = tree.getroot()
Expand Down
Loading