📄 xmlutils.py
字号:
## Copyright (c) 2005, John Mettraux, OpenWFE.org# All rights reserved.# # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met:# # . Redistributions of source code must retain the above copyright notice, this# list of conditions and the following disclaimer. # # . Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution.# # . Neither the name of the "OpenWFE" nor the names of its contributors may be# used to endorse or promote products derived from this software without# specific prior written permission.# # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE.## $Id: xmlutils.py,v 1.9 2005/03/21 15:44:49 jmettraux Exp $#""" Some methods to help parsing/generating XML with minidom $Id: xmlutils.py,v 1.9 2005/03/21 15:44:49 jmettraux Exp $"""from os import environfrom urllib import urlopenfrom xml.dom.minidom import parsedef getContent (elt, subEltName): """ Extracts the text contained in a named subelement of the parameter element. For example, for the element <alpha> <bravo>x-ray</bravo> toto <charly>zoulou</charly> </alpha> the call getContent(element, 'charly') will yield 'zoulou'. """ l = getChildrenByTagName(elt, subEltName) if len(l) < 1: return None subElt = l[0] return extractText(subElt)def extractText (elt): """ Returns the text contained in the element. For example, for the element <toto>nada</toto> the method will return 'nada'. warning : will certainly return an Unicode string """ if (elt.firstChild == None): #print 'extractText didn''t find anything' return '' text = elt.firstChild.data.strip() #print 'text is >%s<' % text #print 'text type is %s' % type(text) return textdef firstChild (elt): """ Returns the element that is the first child of the parameter element. If there are no children in the parameter element, None will be returned. For example, for the element <alpha> <bravo/> <charly/> </alpha> the element <bravo/> will be returned. """ for c in elt.childNodes: if not c.nodeName[0:1] == '#': return c return None def children (elt): """ Returns a list of the children of the parameter element that are elements themselves For example, for the element <alpha> toto <bravo>echo</bravo> <charly/> tutu </alpha> the list containing the elements <bravo>echo</bravo> and <charly/> will be returned. """ l = [] for c in elt.childNodes: if not c.nodeName[0:1] == '#': l.append(c) return ldef getChildrenByTagName (elt, childTagName): """ Returns a list of the direct children of an element that have a given tag name. """ l = [] lchildren = children(elt) for c in lchildren: if c.nodeName == childTagName: l.append(c) return ldef parseUrl (url): """ parse an XML doc located by its URL """ if (url.find(':') < 0): f = open(url) else: f = urlopen(url) return parse(f)## fetchParams()#NAME = 'name'VALUE = 'value'PARAM = 'param'PARAM_NAME = 'param-name'PARAM_VALUE = 'param-value'def fetchParams (elt): result = dict() elts = getChildrenByTagName(elt, PARAM) for e in elts: # # variant 1 : # # <param name="x" value="y" /> # name = e.getAttribute(NAME) value = e.getAttribute(VALUE) if name != None and name != '' and value != '': result[str(name)] = str(value) continue # # variant 2 : # # <param> # <param-name>x</param-name> # <param-value>y</param-value> # </param> # name = getContent(e, PARAM_NAME) value = getContent(e, PARAM_VALUE) if (name != None and value != None): result[str(name)] = str(value) #print result return resultdef fetchAttributes (elt): result = dict() for k in elt._get_attributes().keys(): result[str(k)] = str(elt._get_attributes()[k].value) return resultdef fetchParamsAndAttributes (elt): paramMap = fetchParams(elt) paramMap.update(fetchAttributes(elt)) return paramMap## getEncoding()#def getEncoding (): """ returns the encoding the system should use as found in the environment variable 'OPENWFE_XML_ENCODING' by default, 'ISO-8859-1' is returned """ encoding = environ.get('OPENWFE_XML_ENCODING') if encoding: return encoding return 'ISO-8859-1'## toMinidomString()###def toMinidomString (o):# """# turns anything into an unicode string# """# #if not isinstance(o, basestring): return unicode(o).encode("utf-8")# #print 'toMinidomString() type(o) >%s<' % type(o)# #return o.encode("utf-8")# return unicode(o).encode('utf-8')# #return unicode(o)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -