Skip to content

Instantly share code, notes, and snippets.

@Daemonslayer2048
Last active June 11, 2019 13:29
Show Gist options
  • Save Daemonslayer2048/e5d29e404afa0b94cd0fc405579d2af2 to your computer and use it in GitHub Desktop.
Save Daemonslayer2048/e5d29e404afa0b94cd0fc405579d2af2 to your computer and use it in GitHub Desktop.
Parsing Ugly XML #Python
First a sample XML snippet
``` XML
<settings>
<servers>
<server url="http://speedtest.oppinord.no/speedtest/upload.php" lat="70.6632" lon="23.6817" name="Hammerfest" country="Norway" cc="NO" sponsor="Hammerfest Energi Bredbånd AS" id="21239" host="speedtest.oppinord.no:8080"/>
<server url="http://88.84.191.230/speedtest/upload.php" lat="70.0733" lon="29.7497" name="Vadso" country="Norway" cc="NO" sponsor="Varanger KraftUtvikling AS" id="4600" url2="http://speedmonster.varangerbynett.no/speedtest/upload.php" host="88.84.191.230:8080"/>
</servers>
</settings>
```
Assuming this is a file named ookla.xml. The following code will print the number of "server" tags that exist and print all ID's.
``` Python
from xml.dom import minidom
xmldoc = minidom.parse('ookla.xml')
itemlist = xmldoc.getElementsByTagName('server')
print(len(itemlist))
for s in itemlist:
print(s.attributes['id'].value)
```er!U
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment