summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2002-06-30 15:05:00 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2002-06-30 15:05:00 (GMT)
commit7d650ca83bfdc42e852a4a6af00b80d230ecc54a (patch)
tree3f421d109018e27740476facebbc2fef2e129907 /Lib
parent2ebfd09e5818b7c6d555bcb297ecbb7cf863fe2c (diff)
downloadcpython-7d650ca83bfdc42e852a4a6af00b80d230ecc54a.zip
cpython-7d650ca83bfdc42e852a4a6af00b80d230ecc54a.tar.gz
cpython-7d650ca83bfdc42e852a4a6af00b80d230ecc54a.tar.bz2
Implement the encoding argument for toxml and toprettyxml.
Document toprettyxml.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/output/test_minidom3
-rw-r--r--Lib/test/test_minidom.py8
-rw-r--r--Lib/xml/dom/minidom.py26
3 files changed, 29 insertions, 8 deletions
diff --git a/Lib/test/output/test_minidom b/Lib/test/output/test_minidom
index fc1017b..1612f10 100644
--- a/Lib/test/output/test_minidom
+++ b/Lib/test/output/test_minidom
@@ -98,6 +98,9 @@ Passed assertion: len(Node.allnodes) == 0
Passed Test
Test Succeeded testElementReprAndStr
Passed assertion: len(Node.allnodes) == 0
+Passed testEncodings - encoding EURO SIGN
+Test Succeeded testEncodings
+Passed assertion: len(Node.allnodes) == 0
Test Succeeded testFirstChild
Passed assertion: len(Node.allnodes) == 0
Test Succeeded testGetAttrLength
diff --git a/Lib/test/test_minidom.py b/Lib/test/test_minidom.py
index 4483fc5..d398d73 100644
--- a/Lib/test/test_minidom.py
+++ b/Lib/test/test_minidom.py
@@ -606,6 +606,14 @@ def testSAX2DOM():
doc.unlink()
+def testEncodings():
+ doc = parseString('<foo>&#x20ac;</foo>')
+ confirm(doc.toxml() == u'<?xml version="1.0" ?>\n<foo>\u20ac</foo>'
+ and doc.toxml('utf-8') == '<?xml version="1.0" encoding="utf-8"?>\n<foo>\xe2\x82\xac</foo>'
+ and doc.toxml('iso-8859-15') == '<?xml version="1.0" encoding="iso-8859-15"?>\n<foo>\xa4</foo>',
+ "testEncodings - encoding EURO SIGN")
+ doc.unlink()
+
# --- MAIN PROGRAM
names = globals().keys()
diff --git a/Lib/xml/dom/minidom.py b/Lib/xml/dom/minidom.py
index cb2c4d2..33ad736 100644
--- a/Lib/xml/dom/minidom.py
+++ b/Lib/xml/dom/minidom.py
@@ -65,16 +65,22 @@ class Node(xml.dom.Node):
def __nonzero__(self):
return 1
- def toxml(self):
- writer = _get_StringIO()
- self.writexml(writer)
- return writer.getvalue()
+ def toxml(self, encoding = None):
+ return self.toprettyxml("", "", encoding)
- def toprettyxml(self, indent="\t", newl="\n"):
+ def toprettyxml(self, indent="\t", newl="\n", encoding = None):
# indent = the indentation string to prepend, per level
# newl = the newline string to append
writer = _get_StringIO()
- self.writexml(writer, "", indent, newl)
+ if encoding is not None:
+ import codecs
+ # Can't use codecs.getwriter to preserve 2.0 compatibility
+ writer = codecs.lookup(encoding)[3](writer)
+ if self.nodeType == Node.DOCUMENT_NODE:
+ # Can pass encoding only to document, to put it into XML header
+ self.writexml(writer, "", indent, newl, encoding)
+ else:
+ self.writexml(writer, "", indent, newl)
return writer.getvalue()
def hasChildNodes(self):
@@ -934,8 +940,12 @@ class Document(Node):
return _getElementsByTagNameNSHelper(self, namespaceURI, localName,
NodeList())
- def writexml(self, writer, indent="", addindent="", newl=""):
- writer.write('<?xml version="1.0" ?>\n')
+ def writexml(self, writer, indent="", addindent="", newl="",
+ encoding = None):
+ if encoding is None:
+ writer.write('<?xml version="1.0" ?>\n')
+ else:
+ writer.write('<?xml version="1.0" encoding="%s"?>\n' % encoding)
for node in self.childNodes:
node.writexml(writer, indent, addindent, newl)