Skip to content

XML reader

Bases: BaseReader

A reader class designed to parse Pascal VOC XML annotation files into Python dictionaries.

This class uses the 'xmltodict' library to handle XML data efficiently. It is specifically optimized and tested for stability with XML files generated by the 'labelImg' annotation tool.

Source code in tools/annotation_converter/reader/voc.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class XMLReader(BaseReader):
    """
    A reader class designed to parse Pascal VOC XML annotation files into Python dictionaries.

    This class uses the 'xmltodict' library to handle XML data efficiently. It is
    specifically optimized and tested for stability with XML files generated by
    the 'labelImg' annotation tool.
    """
    def read(self, file_path: Path) -> dict:
        """
        Reads the content of an XML file and converts it into a structured dictionary.

        Args:
            file_path (Path): The path to the source XML file.

        Returns:
            dict: A Python dictionary containing the parsed XML data.

        Raises:
            FileNotFoundError: If the XML file is not found at the given path.
            xmltodict.ParsingInterrupted: If the XML content is not valid or corrupted.
        """
        data = xmltodict.parse(file_path.read_text())
        return data

read(file_path)

Reads the content of an XML file and converts it into a structured dictionary.

Parameters:

Name Type Description Default
file_path Path

The path to the source XML file.

required

Returns:

Name Type Description
dict dict

A Python dictionary containing the parsed XML data.

Raises:

Type Description
FileNotFoundError

If the XML file is not found at the given path.

ParsingInterrupted

If the XML content is not valid or corrupted.

Source code in tools/annotation_converter/reader/voc.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def read(self, file_path: Path) -> dict:
    """
    Reads the content of an XML file and converts it into a structured dictionary.

    Args:
        file_path (Path): The path to the source XML file.

    Returns:
        dict: A Python dictionary containing the parsed XML data.

    Raises:
        FileNotFoundError: If the XML file is not found at the given path.
        xmltodict.ParsingInterrupted: If the XML content is not valid or corrupted.
    """
    data = xmltodict.parse(file_path.read_text())
    return data