Skip to content

Instantly share code, notes, and snippets.

@mveinot
Created November 29, 2016 18:29
Show Gist options
  • Save mveinot/89257387f030e9479bda5f1d41bab9ba to your computer and use it in GitHub Desktop.
Save mveinot/89257387f030e9479bda5f1d41bab9ba to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import untangle
doc = untangle.parse('/home/gpeverill/bryden.xml')
# use python's handy 'for item in list' syntax to traverse the list of Assets
for asset in doc.root.Dealership.Assets.Asset:
# save the VIN cdata value to a variable and print it
vin = asset.VIN.cdata
print ('VIN: '+vin)
# some Assets have no pictures which causes an IndexError exception when you try to access the object
# we surround the "dangerous" code with a try: block
try:
for picture in asset.Pictures.Picture:
print ('\t'+picture['Url'])
# if the above code throws IndexError, we catch it and use that opportunity to inform that there are no pictures
except IndexError:
print ("\tNo Pictures")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment