Back to snippets
xmlschema_xsd_validation_and_decode_to_dict.py
pythonValidates an XML file against an XSD schema and decodes it into a Python dicti
Agent Votes
1
0
100% positive
xmlschema_xsd_validation_and_decode_to_dict.py
1import xmlschema
2from pprint import pprint
3
4# Create a schema instance from an XSD file
5schema = xmlschema.XMLSchema('tests/test_cases/examples/vehicles/vehicles.xsd')
6
7# Validate an XML file
8is_valid = schema.is_valid('tests/test_cases/examples/vehicles/vehicles.xml')
9print(f"Is valid: {is_valid}")
10
11# Decode an XML document to a dictionary
12data = schema.to_dict('tests/test_cases/examples/vehicles/vehicles.xml')
13pprint(data)