1 |
# Copyright (c) 2004 by Intevation GmbH |
2 |
# Authors: |
3 |
# Martin Schulze <[email protected]> |
4 |
# |
5 |
# This program is free software; you can redistribute it and/or modify |
6 |
# it under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 2 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# This program is distributed in the hope that it will be useful, |
11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with this program; if not, write to the Free Software |
17 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
19 |
""" |
20 |
Convenience routines for handling of Document Object Model (DOM) |
21 |
nodes. |
22 |
|
23 |
For more information on DOM see <http://www.zytrax.com/tech/dom/guide.html>. |
24 |
|
25 |
""" |
26 |
|
27 |
def listChildNodes (node): |
28 |
""" |
29 |
Prints a list of all child DOM nodes for the given node. This |
30 |
function is normally only used for inspection of a DOM tree during |
31 |
development. |
32 |
""" |
33 |
print "Node %s has %d children" % (node.nodeName, node.childNodes.length) |
34 |
for i in range (node.childNodes.length): |
35 |
print " %d: %s" % (i, node.childNodes[i].nodeName) |
36 |
|
37 |
|
38 |
def listAttributes (node): |
39 |
""" |
40 |
Prints a list of all DOM attributes for the given node. This |
41 |
function is normally only used for inspection of a DOM tree during |
42 |
development. |
43 |
""" |
44 |
print "Node %s has %d attributes" % (node.nodeName, node.attributes.length) |
45 |
for key in node.attributes.keys(): |
46 |
print " %s=%s" % (key, node.attributes.get(key).nodeValue) |
47 |
|
48 |
|
49 |
# Can't use node.getElementsByTagName(name) since it traverses the XML |
50 |
# data recursively and we need hierarchy information as well in order |
51 |
# to get inheritance of attributes implemented properly. |
52 |
# |
53 |
def getElementsByName (node, name): |
54 |
""" |
55 |
Returns a list of child DOM nodes whose nodeName matches given |
56 |
string. |
57 |
""" |
58 |
res = [] |
59 |
for i in range (node.childNodes.length): |
60 |
if node.childNodes[i].nodeName == name: |
61 |
res.append(node.childNodes[i]) |
62 |
return res |
63 |
|
64 |
|
65 |
def getElementByName (node, name): |
66 |
""" |
67 |
Returns the first child DOM node whose nodeName matches given |
68 |
string. |
69 |
""" |
70 |
try: |
71 |
return getElementsByName (node, name)[0] |
72 |
except IndexError: |
73 |
return None |
74 |
|
75 |
|