-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
198 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
Handling XML (eXtensible Markup Language) files in Python is straightforward, thanks to libraries like `xml.etree.ElementTree` (built-in) and `xml.dom.minidom`. These libraries allow you to parse, read, create, and modify XML data, which is widely used for structured data storage and data exchange. | ||
|
||
Here’s an overview with examples to help you work with XML files in Python. | ||
|
||
### 1. Parsing XML Files | ||
|
||
To parse XML data, the `xml.etree.ElementTree` module provides a simple and efficient way to load an XML file and work with its elements. | ||
|
||
Suppose we have an XML file named `data.xml`: | ||
|
||
```xml | ||
<data> | ||
<person> | ||
<name>Alice</name> | ||
<age>30</age> | ||
<city>Lisbon</city> | ||
</person> | ||
<person> | ||
<name>Bob</name> | ||
<age>25</age> | ||
<city>Porto</city> | ||
</person> | ||
</data> | ||
``` | ||
|
||
#### Example: Parsing XML File | ||
|
||
```python | ||
import xml.etree.ElementTree as ET | ||
|
||
# Parse the XML file | ||
tree = ET.parse('data.xml') | ||
root = tree.getroot() | ||
|
||
# Print the root element | ||
print(root.tag) # Output: data | ||
``` | ||
|
||
### 2. Accessing XML Data | ||
|
||
Once parsed, you can access XML elements using their tags and attributes. You can loop through elements to get individual data values. | ||
|
||
#### Example: Accessing XML Data | ||
|
||
```python | ||
for person in root.findall('person'): | ||
name = person.find('name').text | ||
age = person.find('age').text | ||
city = person.find('city').text | ||
print(f"Name: {name}, Age: {age}, City: {city}") | ||
``` | ||
|
||
**Output**: | ||
``` | ||
Name: Alice, Age: 30, City: Lisbon | ||
Name: Bob, Age: 25, City: Porto | ||
``` | ||
|
||
### 3. Creating and Writing XML Files | ||
|
||
To create an XML structure, you can use `ElementTree.Element` to define elements and then use `ElementTree.ElementTree` to save it to a file. | ||
|
||
#### Example: Creating and Saving XML Data | ||
|
||
```python | ||
import xml.etree.ElementTree as ET | ||
|
||
# Create root element | ||
data = ET.Element("data") | ||
|
||
# Add child elements | ||
person1 = ET.SubElement(data, "person") | ||
ET.SubElement(person1, "name").text = "Alice" | ||
ET.SubElement(person1, "age").text = "30" | ||
ET.SubElement(person1, "city").text = "Lisbon" | ||
|
||
person2 = ET.SubElement(data, "person") | ||
ET.SubElement(person2, "name").text = "Bob" | ||
ET.SubElement(person2, "age").text = "25" | ||
ET.SubElement(person2, "city").text = "Porto" | ||
|
||
# Write to XML file | ||
tree = ET.ElementTree(data) | ||
tree.write("output.xml", encoding="utf-8", xml_declaration=True) | ||
``` | ||
|
||
#### Resulting `output.xml` | ||
|
||
```xml | ||
<?xml version='1.0' encoding='utf-8'?> | ||
<data> | ||
<person> | ||
<name>Alice</name> | ||
<age>30</age> | ||
<city>Lisbon</city> | ||
</person> | ||
<person> | ||
<name>Bob</name> | ||
<age>25</age> | ||
<city>Porto</city> | ||
</person> | ||
</data> | ||
``` | ||
|
||
### 4. Modifying XML Files | ||
|
||
To modify an XML file, you can parse it, make changes, and then save it back. | ||
|
||
#### Example: Modifying XML Data | ||
|
||
Suppose we want to change Bob’s age to 26 in `output.xml`. | ||
|
||
```python | ||
# Parse the XML file | ||
tree = ET.parse('output.xml') | ||
root = tree.getroot() | ||
|
||
# Find Bob's age and update it | ||
for person in root.findall("person"): | ||
name = person.find("name").text | ||
if name == "Bob": | ||
person.find("age").text = "26" | ||
|
||
# Write changes back to the file | ||
tree.write("output_modified.xml") | ||
``` | ||
|
||
#### Resulting `output_modified.xml` | ||
|
||
```xml | ||
<data> | ||
<person> | ||
<name>Alice</name> | ||
<age>30</age> | ||
<city>Lisbon</city> | ||
</person> | ||
<person> | ||
<name>Bob</name> | ||
<age>26</age> | ||
<city>Porto</city> | ||
</person> | ||
</data> | ||
``` | ||
|
||
### 5. Converting XML to Dictionary | ||
|
||
Sometimes, it’s helpful to convert XML data into a Python dictionary for easier manipulation, particularly when working with JSON or other data formats. | ||
|
||
#### Example: XML to Dictionary Conversion | ||
|
||
```python | ||
def xml_to_dict(element): | ||
data = {} | ||
for child in element: | ||
if len(child) > 0: | ||
data[child.tag] = xml_to_dict(child) | ||
else: | ||
data[child.tag] = child.text | ||
return data | ||
|
||
# Convert XML to dictionary | ||
xml_dict = xml_to_dict(root) | ||
print(xml_dict) | ||
``` | ||
|
||
**Output**: | ||
```python | ||
{ | ||
"person": [ | ||
{"name": "Alice", "age": "30", "city": "Lisbon"}, | ||
{"name": "Bob", "age": "26", "city": "Porto"} | ||
] | ||
} | ||
``` | ||
|
||
### 6. Pretty Printing XML | ||
|
||
To make XML more readable, you can use `xml.dom.minidom` for pretty-printing. | ||
|
||
#### Example: Pretty-Printing XML | ||
|
||
```python | ||
import xml.dom.minidom | ||
|
||
# Parse and prettify the XML | ||
xml_str = ET.tostring(root, encoding="utf-8") | ||
parsed_str = xml.dom.minidom.parseString(xml_str) | ||
pretty_xml = parsed_str.toprettyxml(indent=" ") | ||
print(pretty_xml) | ||
``` | ||
|
||
### Summary of Key XML Handling Methods | ||
|
||
- **`ET.parse(file)`**: Parse an XML file. | ||
- **`ET.Element(tag)`**: Create a new XML element. | ||
- **`ET.SubElement(parent, tag)`**: Add a child element. | ||
- **`ET.tostring(element)`**: Convert an XML element to a string. | ||
- **`tree.write(file)`**: Write XML to a file. |