diff --git a/xml_reader.py b/xml_reader.py index 7f4f45f..4e741ea 100644 --- a/xml_reader.py +++ b/xml_reader.py @@ -1,15 +1,30 @@ -import xml.etree.ElementTree as ET - +from lxml import etree class Xml_reader: """ reader for the XML configuration """ - def __init__(self, path): + def __init__(self, xml_path, xsd_path): """inits the reader Args: path (string): path to the XML config file """ - self.root = ET.parse(path).getroot() + #check the XML for validity using the XSD file + if not self.validate(xml_path,xsd_path): + raise SyntaxError('the XML config file has an invalid syntax') + self.root = etree.parse(xml_path).getroot() + + def validate(self, xml_path:str, xsd_path: str) -> bool: + """validates a xml by using a xsd file + + Args: + xml_path (str): the path to the xml file + xsd_path (str): the path to the xsd file + + Returns: + bool: result of the validation + """ + xml_schema = etree.XMLSchema(etree.parse(xsd_path)) + return xml_schema.validate(etree.parse(xml_path)) def get_device_names(self): """returns the names of all devices in the config