Ярлыки

суббота, 3 ноября 2012 г.

XML in Python


#import
from xml.dom import minidom

#parse file
xmldoc=minidom.parse('input.xml')
#get element by tag name
#xmldoc.getElementsByTagName('ID')
#element as list
#xmldoc.getElementsByTagName('ID')[0]
#get child node
#xmldoc.getElementsByTagName('ID')[0].childNodes[0]

#some example
#loop by all child nodes of element with name 'Fragments'
for i in reversed(xmldoc.getElementsByTagName('Fragments')[0].childNodes):
#only elements with name 'Fragment'
    if i.nodeName=='Fragment':
#"number of fragment" - from attribute 'Num'
        fragmentNum=i.getAttribute('Num')
#list in child node
        for j in i.getElementsByTagName('RPs')[0].childNodes:
            if j.nodeName=='RP':
                for u in j.childNodes:
                    if u.nodeName=='Times':
#choose attribute of the node
                        i1=j.getAttribute('Pgsz')

Комментариев нет:

Отправить комментарий