Monday, April 19, 2010

Get attribute name and value of XML using minidom module in Python

#! /usr/bin/python
# -*- coding: utf-8 -*-
import xml.dom.minidom

# your xml string
xmlContent = "..."

# dom parser
dom = xml.dom.minidom.parseString(xmlContent)


# get element with tag "photo"       
photoList = dom.getElementsByTagName("photo")


# get attribute id's value of each 'photo' element       
for photo in photoList:
    print photo.attributes['id'].value



0 comments: