This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a sloppy XML parser library for Python - a single-file XML parser designed to handle malformed XML gracefully while maintaining high performance through pre-compiled regular expressions. The library provides both streaming and tree-building XML parsing capabilities with robust error recovery mechanisms for handling malformed XML commonly generated by LLMs and other automated systems.
This project uses uv as the package manager:
uv sync- Install/update dependencies and sync the environmentuv add <package>- Add a new dependencyuv add --dev <package>- Add a dev only dependencyuv remove <package>- Remove a dependencyuv remove --dev <package>- Remove a dev only dependencyuv run <command>- Run commands in the project environment
uv run pytest- Run all testsuv run pytest tests/test_sloppy_xml.py- Run specific test fileuv run pytest -v- Run tests with verbose outputuv run pytest tests/test_enhanced_recovery.py- Run enhanced recovery tests
uv run ruff check- Check code style and qualityuv run ruff format- Format code automaticallyuv run ruff check --fix- Fix auto-fixable issues
uv build- Build source distribution and wheel packages
The parser implements an event-based streaming architecture with these key components:
StartElement- Opening XML tags with attributes, line/column infoEndElement- Closing XML tags with auto-close detectionText- Text content between tags with CDATA flagComment- XML commentsProcessingInstruction- Processing instructions like<?xml?>EntityRef- Entity references with resolutionParseError- Error events with recovery information
Parser uses enum-based states: INITIAL, IN_TAG, IN_TEXT, IN_COMMENT, IN_CDATA, IN_PI, ERROR_RECOVERY, COMPLETE
- Tag stack management for auto-closing mismatched tags
- Graceful handling of malformed attributes and entities
- CDATA fallback mechanisms
- Entity resolution for HTML entities and numeric entities
stream_parse()- Main streaming parser returning event iteratortree_parse()- Convenience function for ElementTree constructionETreeBuilder- Tree builder for constructing ElementTree objects
sloppy_xml.py- Main parser implementation (single file)tests/test_sloppy_xml.py- Comprehensive test suitetests/test_enhanced_recovery.py- Enhanced error recovery testsARCHITECTURE.md- Detailed architectural specificationTODO.md- Development todo listpyproject.toml- Project configuration with dependencies
See TODO.md for pending tasks. Key areas of active development:
- Optimizing text event emission
- Making ElementTreeBuilder generic for lxml compatibility
- Improving error handling and reporting
- Performance optimizations
The test suite covers:
- Well-formed XML parsing validation
- Malformed XML recovery scenarios
- Entity resolution (standard HTML entities + numeric)
- Performance benchmarks
- Edge cases and boundary conditions
- Real-world malformed XML from LLM outputs