summaryrefslogtreecommitdiffstats
path: root/rteval/xmlout.py
diff options
context:
space:
mode:
authorDavid Sommerseth <dazo@users.sourceforge.net>2009-03-06 11:19:52 +0100
committerDavid Sommerseth <dazo@users.sourceforge.net>2009-03-06 11:19:52 +0100
commit77bad49a59b502862e503ae186194bdb6197163a (patch)
tree6ba3cbea098bc8fe5cb95b66f7796384955773cd /rteval/xmlout.py
parentc861cc94a3df63511e84b16f547cca9d77f349f1 (diff)
downloadrteval-77bad49a59b502862e503ae186194bdb6197163a.tar.gz
rteval-77bad49a59b502862e503ae186194bdb6197163a.tar.xz
rteval-77bad49a59b502862e503ae186194bdb6197163a.zip
Fixed some XML encoding issues
API change on the XMLOut::Write(...) method. It now takes a filename and not a file descriptor. A filename which is set to "-" will be interpreted as stdout in libxml2
Diffstat (limited to 'rteval/xmlout.py')
-rw-r--r--rteval/xmlout.py17
1 files changed, 7 insertions, 10 deletions
diff --git a/rteval/xmlout.py b/rteval/xmlout.py
index 438870e..d039662 100644
--- a/rteval/xmlout.py
+++ b/rteval/xmlout.py
@@ -3,24 +3,21 @@ import os
import sys
import libxml2
-
class XMLOut(object):
'''Class to create XML output'''
def __init__(self, roottag, attr, encoding='UTF-8'):
+ self.encoding = encoding
self.xmldoc = libxml2.newDoc("1.0")
-
self.xmlroot = libxml2.newNode(roottag)
self.__add_attributes(self.xmlroot, attr)
self.currtag = self.xmlroot
self.level = 0
self.closed = False
- self.encoding = encoding
-
def __add_attributes(self, node, attr):
if attr is not None:
for k, v in attr.iteritems():
- node.newProp(k, str(v))
+ node.newProp(k, unicode(str(v), self.encoding))
def close(self):
@@ -32,13 +29,13 @@ class XMLOut(object):
self.closed = True
- def Write(self, file, xslt = None):
+ def Write(self, filename, xslt = None):
if not self.closed:
raise RuntimeError, "XMLOut: XML document is not closed"
if xslt == None:
# If no XSLT template is give, write raw XML
- self.xmldoc.formatDump(file, 1)
+ self.xmldoc.saveFormatFileEnc(filename, self.encoding, 1)
return
@@ -63,7 +60,7 @@ class XMLOut(object):
def taggedvalue(self, tag, value, attributes=None):
- ntag = self.currtag.newTextChild(None, tag, value)
+ ntag = self.currtag.newTextChild(None, tag, value.encode(self.encoding))
self.__add_attributes(ntag, attributes)
@@ -79,6 +76,6 @@ if __name__ == '__main__':
x.closeblock()
x.closeblock()
x.closeblock()
- x.taggedvalue('node2','yet another value', {'shortvalue': "yav"})
+ x.taggedvalue('node2',u'yet another value \xe6\xf8', {'shortvalue': "yav"})
x.close()
- x.Write(sys.stdout)
+ x.Write("-")