File: //opt/PraisonAI/docs/tools/xml_tools.mdx
---
title: "XML Agent"
description: "XML data processing tools for AI agents."
icon: "code"
---
<Note>
**Prerequisites**
- Python 3.10 or higher
- PraisonAI Agents package installed
- `lxml` package installed
</Note>
## XML Tools
Use XML Tools to read, write, and manipulate XML data with AI agents.
<Steps>
<Step title="Install Dependencies">
First, install the required package:
```bash
pip install praisonaiagents
```
</Step>
<Step title="Import Components">
Import the necessary components:
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import read_xml, write_xml, transform_xml, validate_xml, xml_to_dict, dict_to_xml
```
</Step>
<Step title="Create Agent">
Create an XML processing agent:
```python
xml_agent = Agent(
name="XMLProcessor",
role="XML Processing Specialist",
goal="Process XML files efficiently and accurately.",
backstory="Expert in XML file manipulation and validation.",
tools=[read_xml, write_xml, transform_xml, validate_xml, xml_to_dict, dict_to_xml],
self_reflect=False
)
```
</Step>
<Step title="Define Task">
Define the XML processing task:
```python
xml_task = Task(
description="Parse and validate XML configuration files.",
expected_output="Validated and processed XML data.",
agent=xml_agent,
name="xml_processing"
)
```
</Step>
<Step title="Run Agent">
Initialize and run the agent:
```python
agents = PraisonAIAgents(
agents=[xml_agent],
tasks=[xml_task],
process="sequential"
)
agents.start()
```
</Step>
</Steps>
## Understanding XML Tools
<Card title="What are XML Tools?" icon="question">
XML Tools provide XML processing capabilities for AI agents:
- XML parsing and reading
- File writing and validation
- Data transformation
- Dictionary conversion
- Structure manipulation
</Card>
## Key Components
<CardGroup cols={2}>
<Card title="XML Agent" icon="user-robot">
Create specialized XML agents:
```python
Agent(tools=[read_xml, write_xml, transform_xml, validate_xml, xml_to_dict, dict_to_xml])
```
</Card>
<Card title="XML Task" icon="list-check">
Define XML tasks:
```python
Task(description="xml_operation")
```
</Card>
<Card title="Process Types" icon="arrows-split-up-and-left">
Sequential or parallel processing:
```python
process="sequential"
```
</Card>
<Card title="XML Options" icon="sliders">
Customize XML parameters:
```python
encoding="utf-8", pretty_print=True
```
</Card>
</CardGroup>
## Examples
### Basic XML Processing Agent
```python
from praisonaiagents import Agent, Task, PraisonAIAgents
from praisonaiagents.tools import read_xml, write_xml, transform_xml, validate_xml, xml_to_dict, dict_to_xml
# Create XML agent
xml_agent = Agent(
name="XMLExpert",
role="XML Processing Specialist",
goal="Process XML files efficiently and accurately.",
backstory="Expert in XML file handling and validation.",
tools=[read_xml, write_xml, transform_xml, validate_xml, xml_to_dict, dict_to_xml],
self_reflect=False
)
# Define XML task
xml_task = Task(
description="Parse and validate XML configurations.",
expected_output="Processed and validated XML data.",
agent=xml_agent,
name="config_processing"
)
# Run agent
agents = PraisonAIAgents(
agents=[xml_agent],
tasks=[xml_task],
process="sequential"
)
agents.start()
```
### Advanced XML Processing with Multiple Agents
```python
# Create XML processing agent
processor_agent = Agent(
name="Processor",
role="XML Processor",
goal="Process XML files systematically.",
tools=[read_xml, write_xml, transform_xml],
self_reflect=False
)
# Create validation agent
validator_agent = Agent(
name="Validator",
role="Data Validator",
goal="Validate XML structure and content.",
backstory="Expert in data validation and verification.",
tools=[validate_xml, xml_to_dict, dict_to_xml],
self_reflect=False
)
# Define tasks
processing_task = Task(
description="Process and transform XML configurations.",
agent=processor_agent,
name="xml_processing"
)
validation_task = Task(
description="Validate processed XML data.",
agent=validator_agent,
name="data_validation"
)
# Run agents
agents = PraisonAIAgents(
agents=[processor_agent, validator_agent],
tasks=[processing_task, validation_task],
process="sequential"
)
agents.start()
```
## Best Practices
<AccordionGroup>
<Accordion title="Agent Configuration">
Configure agents with clear XML focus:
```python
Agent(
name="XMLProcessor",
role="XML Processing Specialist",
goal="Process XML files accurately and safely",
tools=[read_xml, write_xml, transform_xml, validate_xml, xml_to_dict, dict_to_xml]
)
```
</Accordion>
<Accordion title="Task Definition">
Define specific XML operations:
```python
Task(
description="Parse and validate XML configurations",
expected_output="Validated data structure"
)
```
</Accordion>
</AccordionGroup>
## Common Patterns
### XML Processing Pipeline
```python
# Processing agent
processor = Agent(
name="Processor",
role="XML Processor",
tools=[read_xml, write_xml, transform_xml]
)
# Validation agent
validator = Agent(
name="Validator",
role="Data Validator",
tools=[validate_xml, xml_to_dict, dict_to_xml]
)
# Define tasks
process_task = Task(
description="Process XML files",
agent=processor
)
validate_task = Task(
description="Validate processed data",
agent=validator
)
# Run workflow
agents = PraisonAIAgents(
agents=[processor, validator],
tasks=[process_task, validate_task]
)