
import pandas as pd
# Sample DataFrame
data = {
'id': [1, 2, 3],
'name': ['John Deo', 'Max Ruin', 'Arnold'],
'class': ['Four', 'Three', 'Three'],
'mark': [75, 85, 55],
'gender': ['female', 'male', 'male']
}
df = pd.DataFrame(data)
# Convert to XML
my_xml = df.to_xml('E:\\testing\\data\\student_output.xml',index=False)
### Output:
The file `student_output.xml` will contain:
<?xml version='1.0' encoding='utf-8'?>
<Root>
<Row>
<id>1</id>
<name>John Deo</name>
<class>Four</class>
<mark>75</mark>
<gender>female</gender>
</Row>
<Row>
<id>2</id>
<name>Max Ruin</name>
<class>Three</class>
<mark>85</mark>
<gender>male</gender>
</Row>
<Row>
<id>3</id>
<name>Arnold</name>
<class>Three</class>
<mark>55</mark>
<gender>male</gender>
</Row>
</Root>
---
import pandas as pd
# Sample DataFrame
data = {
'id': [1, 2, 3],
'name': ['John Deo', 'Max Ruin', 'Arnold'],
'class': ['Four', 'Three', 'Three'],
'mark': [75, 85, 55],
'gender': ['female', 'male', 'male']
}
df = pd.DataFrame(data)
# Convert to XML with all attributes explicitly set
df.to_xml(
path_or_buffer='student_custom.xml',
index=False,
root_name='Students',
row_name='Student',
xml_declaration=True, # Include XML declaration
pretty_print=True # Format XML for readability
)
### Output:
<?xml version='1.0' encoding='utf-8'?>
<Students>
<Student>
<id>1</id>
<name>John Deo</name>
<class>Four</class>
<mark>75</mark>
<gender>female</gender>
</Student>
<Student>
<id>2</id>
<name>Max Ruin</name>
<class>Three</class>
<mark>85</mark>
<gender>male</gender>
</Student>
<Student>
<id>3</id>
<name>Arnold</name>
<class>Three</class>
<mark>55</mark>
<gender>male</gender>
</Student>
</Students>
---
| Attribute | Default Value | Description |
|---|---|---|
path_or_buffer |
None | The file path or buffer where the XML output will be written. |
index |
True | Whether to include the DataFrame index in the XML output. |
root_name |
'Root' | The name of the root element in the XML. |
row_name |
'Row' | The name of each row element in the XML. |
xml_declaration |
True | Whether to include the XML declaration at the beginning of the file. |
pretty_print |
True | Format the XML output for better readability. |
df.to_xml('student_with_index.xml',index=True)
### Output:
<?xml version='1.0' encoding='utf-8'?>
<Root>
<Row>
<index>0</index>
<id>1</id>
<name>John Deo</name>
<class>Four</class>
<mark>75</mark>
<gender>female</gender>
</Row>
<Row>
<index>1</index>
<id>2</id>
<name>Max Ruin</name>
<class>Three</class>
<mark>85</mark>
<gender>male</gender>
</Row>
</Root>
---
from sqlalchemy import create_engine
import pandas as pd
# Step 1: Connect to the SQLite database using SQLAlchemy
engine = create_engine('sqlite:///F:/testing/sqlite/my_db.db') # Replace with your database path
# Step 2: Read the student table into a DataFrame
query = 'SELECT * FROM student'
df = pd.read_sql_query(query, engine.connect())
# Step 3: Export the DataFrame to an XML file
df.to_xml(
path_or_buffer='F:/testing/sqlite/student.xml',
parser='etree',
index=False,
root_name='Students',
row_name='Student',
xml_declaration=True, # Include XML declaration
pretty_print=True # Format XML for readability
)
# Print success message
print("Data successfully exported to student.xml")
to_xml() function in Pandas?to_xml()?index parameter in to_xml()?
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.