Metadata-Version: 2.4
Name: xml-checker
Version: 1.0.0
Summary: A Python toolkit for parsing and comparing XML files.
Author: Wing Kui Tsoi
License-Expression: MIT
Project-URL: Homepage, https://github.com/wingkuitsoi/xml-checker
Project-URL: Repository, https://github.com/wingkuitsoi/xml-checker
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Operating System :: OS Independent
Classifier: Topic :: Text Processing :: Markup :: XML
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# xml-checker

A Python toolkit for parsing and comparing XML files.

## Overview

| Script | Purpose |
|--------|---------|
| `xml_parser.py` | Parse XML/text files into `ET.Element` objects with robust pre-processing |
| `xml_diff.py` | Compare two parsed XML objects and produce a structured diff report |

## Requirements

- Python 3.12+
- Standard library only (no third-party dependencies)

---

## xml_parser.py

Reads an XML or text file and returns a list of top-level `ET.Element` objects.
Files with multiple root-level elements are supported by wrapping content in a temporary `<_root_>` element before parsing.

**Pre-processing pipeline** (applied in order before parsing):

| Step | What is removed |
|------|----------------|
| 1 | `<!-- comments -->` |
| 2 | `<!DOCTYPE ...>` declarations (including internal DTD subsets) |
| 3 | Processing instructions `<?...?>` (the XML declaration `<?xml ...?>` is kept until step 4) |
| 4 | XML declaration `<?xml ...?>` |
| 5 | Custom `&entity;` references (built-ins `&amp;`, `&lt;`, `&gt;`, `&apos;`, `&quot;` are preserved) |

Namespace URIs are stripped from all tag names and attribute keys after parsing.

### CLI

```
python xml_parser.py <file.xml|file.txt> [output.txt]
```

Output file defaults to `output.txt`.

### Python API

```python
from xml_parser import parse_file, display_elements, save_elements_to_file

elements = parse_file("input.xml")           # list[ET.Element]
display_elements(elements)                   # print to stdout
save_elements_to_file(elements, "out.txt")   # write to file
```

---

## xml_diff.py

Compares two XML objects and generates a detailed diff report covering:

- **Summary** – total tags, tags exclusive to each object, common tags, and total attribute differences.
- **Tag differences** – element paths present in one object but not the other.
- **Attribute differences** – per-element, per-attribute value comparison (grouped by depth level).

### CLI

```
python xml_diff.py <file1.xml|file1.txt> <file2.xml|file2.txt> [output.txt] [--tag-key=<attr>]
```

| Argument | Required | Description |
|----------|----------|-------------|
| `file1` | Yes | First XML file |
| `file2` | Yes | Second XML file |
| `output.txt` | No | Report output path (default: `output.txt`) |
| `--tag-key=<attr>` | No | Attribute used to disambiguate sibling elements in paths (default: `id`) |

### Python API

```python
from xml_parser import parse_file
from xml_diff import build_report, save_report

obj1 = parse_file("a.xml")
obj2 = parse_file("b.xml")

report = build_report(obj1, obj2, tag_key="id")
print(report)
save_report(report, "output.txt")
```

---

## Sample Files

The `samples/` folder contains example files organised by use case.

### Parser samples — `samples/parser/`

| Input | Expected output | Description |
|-------|----------------|-------------|
| `simple.xml` | `simple_output.txt` | Basic XML with nested elements and attributes |
| `complex.xml` | `complex_output.txt` | Demonstrates all pre-processing: comments, DOCTYPE, processing instructions, namespaces, and entity references |

```
python xml_parser.py samples/parser/simple.xml  samples/parser/simple_output.txt
python xml_parser.py samples/parser/complex.xml samples/parser/complex_output.txt
```

### Diff samples — `samples/diff/`

| File pair | Expected report | Scenario |
|-----------|----------------|----------|
| `empty_a.xml` / `empty_b.xml` | `empty_report.txt` | Both files contain no XML elements → `"Both objects are empty, so there is nothing to compare."` |
| `same_a.xml` / `same_b.xml` | `same_report.txt` | Identical XML structure and attributes → no differences reported |
| `different_a.xml` / `different_b.xml` | `different_report.txt` | Server configs with differing hosts, ports, drivers, and feature sets → full diff report |

```
python xml_diff.py samples/diff/empty_a.xml     samples/diff/empty_b.xml     samples/diff/empty_report.txt
python xml_diff.py samples/diff/same_a.xml      samples/diff/same_b.xml      samples/diff/same_report.txt
python xml_diff.py samples/diff/different_a.xml samples/diff/different_b.xml samples/diff/different_report.txt
```
