Skip to content

Commit ba4e686

Browse files
Use defusedxml for Parsing XML
1 parent 03f4992 commit ba4e686

File tree

9 files changed

+22
-21
lines changed

9 files changed

+22
-21
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pytest = "^7.1.3"
1313
requests = "^2.28.1"
1414
jupyter = "^1.0.0"
1515
ipython = "^8.5.0"
16+
defusedxml = "==0.7.1"
1617

1718

1819
[tool.poetry.group.dev.dependencies]

python3/11_File_Operations/02_structured_files/02_xml/01_xml/a_write_xml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
# print(dir(xml))
2323

2424
import xml.etree.ElementTree as ET
25-
from xml.dom import minidom
25+
import defusedxml.minidom
2626

2727
# print(dir(ET))
2828

@@ -42,7 +42,7 @@
4242
print()
4343

4444

45-
result_str2 = minidom.parseString(
45+
result_str2 = defusedxml.minidom.parseString(
4646
ET.tostring(root)
4747
).toprettyxml()
4848

python3/11_File_Operations/02_structured_files/02_xml/01_xml/c_parse_xml.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""
22
Purpose: Reading(Parsing) XML
33
"""
4-
5-
import xml.etree.ElementTree as ET
64
from pprint import pp
5+
import defusedxml.ElementTree
76

8-
tree = ET.parse("books.xml")
7+
tree = defusedxml.ElementTree.parse("books.xml")
98

109
# print(dir(tree))
1110

python3/11_File_Operations/02_structured_files/02_xml/01_xml/d_parse_xml_string.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"""
22
Purpose: To parse(read) xml string
33
"""
4-
5-
import xml.etree.ElementTree as ET
4+
import defusedxml.ElementTree
65

76
input_string = """
87
<stuff>
@@ -18,7 +17,7 @@
1817
</users>
1918
</stuff>"""
2019

21-
stuff_tree = ET.fromstring(input_string)
20+
stuff_tree = defusedxml.ElementTree.fromstring(input_string)
2221

2322
nodes = stuff_tree.findall("users") # child level
2423
print(nodes)

python3/11_File_Operations/02_structured_files/02_xml/01_xml/e_parse_xml_string.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/python3
22

3-
import xml.etree.ElementTree as ElementTree
3+
import defusedxml.ElementTree
44

55
data = """
66
<person>
@@ -11,6 +11,6 @@
1111
<email hide="yes"/>
1212
</person>"""
1313

14-
tree = ElementTree.fromstring(data)
14+
tree = defusedxml.ElementTree.fromstring(data)
1515
print("Name:", tree.find("name").text)
1616
print("Attr:", tree.find("email").get("hide"))

python3/16_Web_Services/c_REST/a_consuming_APIs/02_requests/requests-workshop-master/answers/answers_05.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import xml.etree.ElementTree as et
22

33
import requests
4+
import defusedxml.ElementTree
45

56

67
# Exercise 5.1
@@ -14,7 +15,7 @@ def test_check_root_of_xml_response():
1415
response = requests.get(
1516
"http://parabank.parasoft.com/parabank/services/bank/accounts/12345", timeout=60
1617
)
17-
response_body_as_xml = et.fromstring(response.content)
18+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
1819
xml_tree = et.ElementTree(response_body_as_xml)
1920
root = xml_tree.getroot()
2021
assert root.tag == "account"
@@ -32,7 +33,7 @@ def test_check_specific_element_of_xml_response():
3233
response = requests.get(
3334
"http://parabank.parasoft.com/parabank/services/bank/accounts/12345", timeout=60
3435
)
35-
response_body_as_xml = et.fromstring(response.content)
36+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
3637
xml_tree = et.ElementTree(response_body_as_xml)
3738
first_name = xml_tree.find("customerId")
3839
assert first_name.text == "12212"
@@ -49,7 +50,7 @@ def test_check_number_of_accounts_for_12212_greater_than_five():
4950
"http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts",
5051
timeout=60,
5152
)
52-
response_body_as_xml = et.fromstring(response.content)
53+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
5354
xml_tree = et.ElementTree(response_body_as_xml)
5455
accounts = xml_tree.findall(".//account")
5556
assert len(accounts) > 5
@@ -66,7 +67,7 @@ def test_use_xpath_for_more_sophisticated_checks():
6667
"http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts",
6768
timeout=60,
6869
)
69-
response_body_as_xml = et.fromstring(response.content)
70+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
7071
xml_tree = et.ElementTree(response_body_as_xml)
7172
savings_accounts = xml_tree.findall(".//account/type[.='SAVINGS']")
7273
assert len(savings_accounts) > 1

python3/16_Web_Services/c_REST/a_consuming_APIs/02_requests/requests-workshop-master/examples/examples_05.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import xml.etree.ElementTree as et
22

33
import requests
4+
import defusedxml.ElementTree
45

56

67
def test_check_root_of_xml_response():
78
response = requests.get(
89
"http://parabank.parasoft.com/parabank/services/bank/customers/12212",
910
timeout=60,
1011
)
11-
response_body_as_xml = et.fromstring(response.content)
12+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
1213
xml_tree = et.ElementTree(response_body_as_xml)
1314
root = xml_tree.getroot()
1415
assert root.tag == "customer"
@@ -20,7 +21,7 @@ def test_check_specific_element_of_xml_response():
2021
"http://parabank.parasoft.com/parabank/services/bank/customers/12212",
2122
timeout=60,
2223
)
23-
response_body_as_xml = et.fromstring(response.content)
24+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
2425
xml_tree = et.ElementTree(response_body_as_xml)
2526
first_name = xml_tree.find("firstName")
2627
assert first_name.text == "John"
@@ -33,7 +34,7 @@ def test_use_xpath_for_more_sophisticated_checks():
3334
"http://parabank.parasoft.com/parabank/services/bank/customers/12212",
3435
timeout=60,
3536
)
36-
response_body_as_xml = et.fromstring(response.content)
37+
response_body_as_xml = defusedxml.ElementTree.fromstring(response.content)
3738
xml_tree = et.ElementTree(response_body_as_xml)
3839
address_children = xml_tree.findall(".//address/*")
3940
assert len(address_children) == 4

python3/16_Web_Services/c_REST/a_consuming_APIs/g_downloading_files/get_xml_data.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from numbers import Number
22
from typing import Optional
3-
from xml.dom.minidom import parseString
43

54
import requests
5+
import defusedxml.minidom
66

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

2121
# we have to parse XML (unfortunately I did not find a .json API)
22-
parsed = parseString(
22+
parsed = defusedxml.minidom.parseString(
2323
str(res.content.decode("utf-8")).replace("\n", "").replace("\t", "")
2424
)
2525
currency_rates = parsed.childNodes[0].childNodes[2].childNodes[0].childNodes

python3/16_Web_Services/h_feedparsing/parse_RSS_feed.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Python code to illustrate parsing of XML files
22
# importing the required modules
33
import csv
4-
import xml.etree.ElementTree as ET
54

65
import requests
6+
import defusedxml.ElementTree
77

88

99
def loadRSS():
@@ -20,7 +20,7 @@ def loadRSS():
2020

2121
def parseXML(xmlfile):
2222
# create element tree object
23-
tree = ET.parse(xmlfile)
23+
tree = defusedxml.ElementTree.parse(xmlfile)
2424

2525
# get root element
2626
root = tree.getroot()

0 commit comments

Comments
 (0)