How to parse xml in python?

Hello!
Guys, tell me how correctly to steam xml document using PYTHON?
Just starting out and can't find a good example.
I would like to deal in practice with the review.

Here is my document defaultVar.xml

<?xml version = "1.0" encoding = "UTF-8"?>
<repository xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://www.tibco.com/xmlns/repo/types/2002">
<globalVariables>
<globalVariable>
<name>Host</name>
<value>10.64.13.131:1576</value>
<deploymentSettable>true</deploymentSettable>
<serviceSettable>false</serviceSettable>
<type>String</type>
<modTime>1559628965605</modTime>
</globalVariable>
<globalVariable>
<name>MaxConnections</name>
<value>10</value>
<deploymentSettable>true</deploymentSettable>
<serviceSettable>false</serviceSettable>
<type>Integer</type>
<modTime>1559628965605</modTime>
</globalVariable>
<globalVariable>
<name>SID</name>
<value>taffy</value>
<deploymentSettable>true</deploymentSettable>
<serviceSettable>false</serviceSettable>
<type>String</type>
<modTime>1559628965605</modTime>
</globalVariable>
</globalVariables>
</repository>


Can someone show how to choose tags SID, HOST (and all their attributes!).
Does not reach the head. If possible, with comments.
Thank you!
March 23rd 20 at 19:24
2 answers
March 23rd 20 at 19:26
Removed the xmlns for easier parsing
Use a standard libw running with Python
import xml.etree.ElementTree as ET
tree = ET.fromstring("xml_string") # or ET.parse("path_to_xml_file")
for element in tree.findall("globalVariables/globalVariable"): # or tree.findall('globalVariables/globalVariable/name')
 name = element.find("name")
 print(element.tag name.text, element.attrib)

But better read the documentation: https://docs.python.org/3.7/library/xml.etree.elem...
Hi!
Try to use this way:
import xml.etree.ElementTree as ET
tree = ET.parse("C:/Users/admin/Documents/Python/defaultVars.xml")
for element in tree.findall("globalVariables/globalVariable"):
name = element.find("SID")
print(element.tag name.text, element.attrib)

Retained as parcing.py
Start python.exe parcing.py using cmd
The execution result is empty.
Can you tell why? - Lori_Runolfsdott commented on March 23rd 20 at 19:29
@Lori_Runolfsdott,
import xml.etree.ElementTree as ET
tree = ET.parse("C:/Users/admin/Documents/Python/defaultVars.xml")
for element in tree.findall("globalVariables/globalVariable"):
 name = element.find("SID") # Incorrectly, specify a tag name, but not the text which it is
 name = element.find("name") # Right, Looking for a tag name which should be a required text
 print(name.tag name.attrib, name.text)

From now on, try to execute the code - Olaf_Beatty36 commented on March 23rd 20 at 19:32
March 23rd 20 at 19:28
What a dumb site? - domenica.Kuhic commented on March 23rd 20 at 19:31
@domenica.Kuhic, what's with him? - Margarett.Wel commented on March 23rd 20 at 19:34
@Margarett.Wel, not off site lxml - domenica.Kuhic commented on March 23rd 20 at 19:37
@domenica.Kuhic, I think off-site to hinder the understanding of the questioner in connection with the documentation is not in Russian. Well, this is the first that has fallen in the search engines, because in my opinion, the issue of discharge "is Easily found with a search engine" - Margarett.Wel commented on March 23rd 20 at 19:40

Find more questions by tags Python