Skip to content

XML Parser Reference

The parse_xml function unpakcs a relatics XML into a pandas dataframe. The XML relatics returns is deeply nested and needs to be unpacked, multiple report parts can be part of the same report. This means that multible different tables can be formed from a single webservice. In order to cleanly extract these tables the report part should be specified. This means the report part as defined in the Relatics report.

Overview

relatics_toolkit.parse_xml(root, report_part)

Function to convert a relatics report part into a pandas DataFrame.

Relatics report parts can be deeply nested, this function recusively unpacks the XML and returns a single DataFrame.

Parameters:

Name Type Description Default
root ElementTree

The complete xml.etree.ElementTree xml as obtained from the relatics webservice. Can be easily obtained from the RelaticsClient.

required
report_part str

Specific report part to unpack into a pandas dataframe.

required

Returns:

Name Type Description
df DataFrame

An unpacked pandas DataFrame of a specific report part.

Source code in src/relatics_toolkit/ingestion/xml_parser.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
30
31
32
33
34
35
def parse_xml(root:ET.Element, report_part:str) -> pd.DataFrame:
    """Function to convert a relatics report part into a pandas DataFrame.

    Relatics report parts can be deeply nested, this function recusively unpacks the XML and returns a single DataFrame.

    Args:
        root (xml.etree.ElementTree): The complete xml.etree.ElementTree xml as obtained from the relatics webservice. Can be easily obtained from the RelaticsClient.
        report_part: Specific report part to unpack into a pandas dataframe.

    Returns:
        df: An unpacked pandas DataFrame of a specific report part.

    """
    start_element = root.find(report_part)

    nested_rows = []

    if start_element is not None:
        nested_rows.append(_parse_xml_to_dict(start_element=start_element))

    unpacked_rows = []

    for row in nested_rows:
        unpacked_rows.extend(_recursive_unpack(row))

    df = pd.DataFrame(unpacked_rows)

    df.columns = [col.split('.')[-1] for col in df.columns]

    return df