[Python] Convert XML to JSON using xmltodict package

Discussão em 'Python' iniciado por Stack, Agosto 11, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to convert the below XML file to JSON using xmltodict package but getting Expat syntax error

    abc.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <testsuites tests="1" failures="0" disabled="0" errors="0" time="0.213" timestamp="2025-06-22T17:35:10.404" name="AllTests">
    <testsuite name="qw" tests="4" failures="0" disabled="0" skipped="0" errors="0" time="0.213" timestamp="2025-06-22T17:35:10.404">
    <testcase name="er" file="" line="" status="run" result="PASSED" time="0.0" timestamp="2025-06-22T17:35:10.404" classname="qw" />
    </testsuite>
    </testsuites>


    main.py:

    import xmltodict, json

    def main():

    o = xmltodict.parse("abc.xml")
    json.dumps(o)

    if __name__ == "__main__":
    main()


    Error:

    Traceback (most recent call last):
    File "/home/harry/xml_to_json/main.py", line 9, in <module>
    main()
    File "/home/harry/xml_to_json/main.py", line 5, in main
    o = xmltodict.parse("abc.xml")
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/harry/xml_to_json/.venv/lib/python3.12/site-packages/xmltodict.py", line 359, in parse
    parser.Parse(xml_input, True)
    xml.parsers.expat.ExpatError: syntax error: line 1, column 0

    Continue reading...

Compartilhe esta Página