Last active
June 11, 2019 13:29
-
-
Save Daemonslayer2048/e5d29e404afa0b94cd0fc405579d2af2 to your computer and use it in GitHub Desktop.
Parsing Ugly XML #Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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