How to read xml file in python? - stack overflow

I I have a xml file like this



    
        .\SERVER.log
        .\Flag
        .\PO
    


I want to read the xml file and get the element of those, and assign to variable.

I tried this, but I can not get the element of Log.

import xml.dom.minidom
def main []:
    Load_XML = xml.dom.minidom.parse['D:/Config.xml']
    print [Load_XML.nodeName]
    print [Load_XML.firstChild.tagName]

    Log = Load_XML.getElementsByTagName ["Log"]
    print [Log]

main[]

mzjn

46.6k11 gold badges122 silver badges237 bronze badges

asked Aug 5, 2019 at 3:34

3

Use ElementTree:

import xml.etree.ElementTree as ET
tree = ET.parse['Config.xml']
root = tree.getroot[]
print[root.findall['.//Log']]

Output:

pawel@pawel-XPS-15-9570:~/test$ python parse_xml.py 
[

Chủ Đề