0

How can I correctly parse a XML file (config.xml) with elementtree?

With the function getting_config_from_file() (the goal is to retreive a list with all the subjects ids to use later. But this function actually doesn't return anything. I also think that for some reason it is not even entering the for statement. What could I be doing differently?

here is my function:

def getting_config_from_file():
    tree = ET.parse('config.xml')
    root = tree.getroot()
    parsed_subjects = []
    for x in root[0]:
        parsed_subjects.append(x.attrib)
    print(parsed_subjects)
    return parsed_subjects

and here is what my xml file looks like

<?xml version="1.0" encoding="UTF-8" ?>
<all_configs>
    <subjects>
        <subject>
            <id>EB101</id>
            <subject_name>Cálculo I</subject_name>
            <classes_no>1</classes_no>
            <pre_reqs></pre_reqs>
        </subject>
        <subject>
            <id>SI100</id>
            <subject_name>Algoritmos e Programação de Computadores I</subject_name>
            <classes_no>2</classes_no>
            <pre_reqs></pre_reqs>
        </subject>
        <subject>
            <id>SI120</id>
            <subject_name>Lógica Matemática</subject_name>
            <classes_no>1</classes_no>
            <pre_reqs></pre_reqs>
        </subject>
        <subject>
            <id>SI201</id>
            <subject_name>Estrutura de Dados I</subject_name>
            <classes_no>2</classes_no>
            <pre_reqs></pre_reqs>
        </subject>
        <subject>
            <id>SI250</id>
            <subject_name>Economia e Finanças</subject_name>
            <classes_no>3</classes_no>
            <pre_reqs>SI100</pre_reqs>
        </subject>
    </subjects>
    <parameters>
        <parameter>
            <parameter_name>Below Average</parameter_name>
            <min_grade>0</min_grade>
            <max_grade>5</max_grade>
        </parameter>
        <parameter>
            <parameter_name>Average</parameter_name>
            <min_grade>5</min_grade>
            <max_grade>7</max_grade>
        </parameter>
        <parameter>
            <parameter_name>Above Average</parameter_name>
            <min_grade>7</min_grade>
            <max_grade>10</max_grade>
        </parameter>
    </parameters>
</all_configs>
</xml>

1 Answer 1

1

The xml file should not have </xml> in the end, and it may not parse as a result. Notwithstanding that, .attrib will look for the attributes of the element. subject element does not have any attribute and as a result, an empty list is returned.

To get the id for every subject, we need to access first element inside the for loop. Here is how it may look like:

 def getting_config_from_file():
    tree = ET.parse('config.xml')
    root = tree.getroot()
    parsed_subjects = []
    for x in root[0]: # access each subject
        parsed_subjects.append(x[0].text) # every x is an element. 0 refers to the first element.
    print(parsed_subjects)
    return parsed_subjects

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.