I I have a xml file like this
<?xml version="1.0" encoding="UTF-8"?>
<Automation_Config>
<Path>
<Log>.\SERVER.log</Log>
<Flag_Path>.\Flag</Flag_Path>
<files>.\PO</files>
</Path>
</Automation_Config>
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()
xml.dom.minidom
. Unless you have a very specific need to work with the very minimal and basic W3C DOM API, you want to be using thexml.etree.ElementTree
API instead. The DOM API is aimed as a minimal common ground between lots of programming languages, many not nearly as expressive as Python. As a consequence, it is very, very, very tedious to have to work with.ElementTree
API, getting the string contents of the firstLog
element is as trivial astree = ET.parse(filename)
thenvalue = tree.find(".//Log").text
.