Initial revision
[gnucomo.git] / src / coconf / coxmlfile.py
1 #   Copyright (C) 2002, Peter Roozemaal.
2 #
3 # GnuCoMo/CoConf is free software; you may redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2, or (at your option)
6 # any later version.  See the file COPYING.
7
8 import string
9 from xml.dom.minidom import parse, Document
10
11 _id = "$Id: coxmlfile.py,v 1.1 2002-11-19 19:21:18 arjen Exp $"
12
13 def emptytext(node):
14     return node.nodeType == node.TEXT_NODE and string.strip(node.data) == ""
15
16 # NOTE: the normalise function doesn't work under python 2.0.
17 def normalise(xmlnode):
18     "We strip blanks-only text nodes"
19     child = xmlnode.firstChild
20     while child != None:
21         if emptytext(child):
22             remove = child
23             child = child.nextSibling
24             xmlnode.removeChild(remove)
25             remove.unlink()
26         else:
27             if child.hasChildNodes():
28                 normalise(child)
29             child = child.nextSibling
30     return xmlnode;
31
32 def load_from_file(filename):
33     file = open(filename, "r")
34     dom = parse(file)
35     file.close()
36 #   return normalise(dom)
37     return dom
38
39 def save_to_file(dom, filename):
40     file = open(filename, "w")
41     dom.writexml(file)
42     file.close()