In [ ]:
'''
from lxml import etree
fromstring(), tostring()
 .tag, .text, .attrib[name]
'''
In [1]:
from lxml import etree
In [10]:
with open('getBusesForRoute.xml') as f:
    doc = etree.fromstring( f.read() )
In [11]:
type(doc)
Out[11]:
lxml.etree._Element
In [12]:
for item in doc:
    print( type(item) )
<class 'lxml.etree._Comment'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
<class 'lxml.etree._Element'>
In [14]:
for item in doc[:4]:
    print( item.tag, item.attrib )
    for jtem in item:
        print('\t',jtem.tag)
<cyfunction Comment at 0x7fc44c6f5c80> <lxml.etree._ImmutableMapping object at 0x7fc44c731588>
time {}
bus {}
	 id
	 consist
	 cars
	 rtpiFeedName
	 m
	 rt
	 rtRtpiFeedName
	 rtdd
	 d
	 dd
	 dn
	 lat
	 lon
	 pid
	 pd
	 pdRtpiFeedName
	 run
	 fs
	 op
	 dip
	 bid
	 wid1
	 wid2
bus {}
	 id
	 consist
	 cars
	 rtpiFeedName
	 m
	 rt
	 rtRtpiFeedName
	 rtdd
	 d
	 dd
	 dn
	 lat
	 lon
	 pid
	 pd
	 pdRtpiFeedName
	 run
	 fs
	 op
	 dip
	 bid
	 wid1
	 wid2
In [15]:
with open('simple.svg') as f:
    doc = etree.fromstring( f.read() )
for item in doc:
    print( item.tag )
    print('\t',item.attrib )
{http://www.w3.org/2000/svg}circle
	 {'cx': '50', 'stroke': 'gray', 'stroke-width': '8', 'fill': '#77cc77', 'cy': '50', 'r': '40'}
In [ ]: