《Dive into Python》学习笔记
http://blog.csdn.net/lanphaday/archive/2008/08/31/2857813.aspx
27: xml
1. import:
/>>>from xml.dom import minidom –import an package xml with its dom (also:from xml import dom)
/>>>minidom / xml, dom
2. xml file:
/>>>xmldoc = minidom.parse(’/home/bwar/python/diveintopythonzh-cn-5.4b/py/kgp/binary.xml’) –import an xml file
/>>>print xmldoc.toxml() –print all
3. childNodes:
/>>>xmldoc.childNode — to display all the nodes
/>>>xmldoc.childNode[-1][0][1][2] / xmldoc.firstChild –display nodes one by one
/>>>grammarNode = xmldoc.firstChild
/>>>print grammarNode.toxml()
3. keyword:
/>>>reflist = xmldoc.getElementsByTagName(’preface’)
4. print:
/>>>print reflist.toxml() ##also:print reflist[1].toxml()##
5. print inside:
5.1:
/>>>plist = reflist.getElementsByTagName(’preface’)
5.2:
/>>>plist = xmldoc.getElementsByTagName(’preface’)
6. visite the elements:
/>>>bitref = reflist[0]
/>>>print bitref.toxml()
/>>>bitref.attributes
/>>>bitref.attributes.keys()
/>>>bitref.attributes.values()
/>>>bitref.attributes["id"]
/>>>a = bitref.attributes["id"]
/>>>a
/>>>a.name
/>>>a.value
To get os path
/>>>import os
/>>> for k, v in os.environ.items():
… print “%s=%s” % (k, v)
or
/>>>import os
/>>> print “\n”.join(["%s=%s" % (k, v)
... for k, v in os.environ.items()])
28. HTTP web:
1) old method:
/>>>import urllib
/>>>data = urllib.urlopen(’http://diveintomark.org/xml/atom.xml’).read()
/>>>print data
2) new method with urllib2:
/>>>import urllib2
/>>>httplib.HTTPConnection.debuglevel = 1
/>>>import urllib2
/>>>request = urllib2.Request(’http://diveintomark.org/xml/atom.xml’)
/>>>opener = urllib2.build_opener()
/>>>feeddata = opener.open(request).read()
/>>>request.get_full_url()
/>>>request.add_header(’User-Agent’, ‘OpenAnything/1.0 +http://diveintopython.org/’) ####add more info
/>>>firstdatastream = opener.open(request)
/>>>firstdatastream.headers.dict
/>>>request.headers