def openAnything(source):
# try open with urllib (http, ftp, file URL)
import urllib
try:
return urllib.urlopen(source)
except (IOError, OSError):
pass
# try open with native open
try:
return open(source)
except (IOError, OSError):
pass
# treat source as string
import StringIO
return StringIO.StringIO(str(source))
Now you can use this openAnything function in conjunction with minidom.parse to make a function that takes
a source that refers to an XML document somehow (either as a URL, or a local filename, or a hard−coded XML
document in a string) and parses it.
def _load(self, source):
sock = toolbox.openAnything(source)
xmldoc = minidom.parse(sock).documentElement
sock.close()
return xmldoc
# try open with urllib (http, ftp, file URL)
import urllib
try:
return urllib.urlopen(source)
except (IOError, OSError):
pass
# try open with native open
try:
return open(source)
except (IOError, OSError):
pass
# treat source as string
import StringIO
return StringIO.StringIO(str(source))
Now you can use this openAnything function in conjunction with minidom.parse to make a function that takes
a source that refers to an XML document somehow (either as a URL, or a local filename, or a hard−coded XML
document in a string) and parses it.
def _load(self, source):
sock = toolbox.openAnything(source)
xmldoc = minidom.parse(sock).documentElement
sock.close()
return xmldoc
Comments
Post a Comment
https://gengwg.blogspot.com/