summaryrefslogtreecommitdiff
path: root/beautifulsoup/__init__.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonard.richardson@canonical.com>2011-02-26 20:57:51 -0500
committerLeonard Richardson <leonard.richardson@canonical.com>2011-02-26 20:57:51 -0500
commit1f4733fd1215411752c4d793d93edbc007fece12 (patch)
treeac9313b90f2fb5b0e93d5c1a4788c5a4ba0b523e /beautifulsoup/__init__.py
parent7db58f67d60bf2675613e38d2d8daeeadc5522db (diff)
parentef5770589595e80cbd9690b64504a2166b3558fd (diff)
Emit an XML declaration when appropriate. I'm not totally happy with this, but it's good enough for now.
Diffstat (limited to 'beautifulsoup/__init__.py')
-rw-r--r--beautifulsoup/__init__.py18
1 files changed, 17 insertions, 1 deletions
diff --git a/beautifulsoup/__init__.py b/beautifulsoup/__init__.py
index ce39d33..cee55e7 100644
--- a/beautifulsoup/__init__.py
+++ b/beautifulsoup/__init__.py
@@ -66,7 +66,7 @@ import re
from util import isList, buildSet
from builder import builder_registry
from dammit import UnicodeDammit
-from element import NavigableString, Tag
+from element import DEFAULT_OUTPUT_ENCODING, NavigableString, Tag
class BeautifulSoup(Tag):
@@ -122,6 +122,7 @@ class BeautifulSoup(Tag):
% ",".join(features))
builder = builder_class()
self.builder = builder
+ self.is_xml = builder.is_xml
self.builder.soup = self
self.parse_only = parse_only
@@ -261,6 +262,21 @@ class BeautifulSoup(Tag):
def handle_data(self, data):
self.currentData.append(data)
+ def decode(self, pretty_print=False, indent_level=0,
+ eventual_encoding=DEFAULT_OUTPUT_ENCODING):
+ """Returns a string or Unicode representation of this document.
+ To get Unicode, pass None for encoding."""
+ if self.is_xml:
+ # Print the XML declaration
+ encoding_part = ''
+ if eventual_encoding != None:
+ encoding_part = ' encoding="%s"' % eventual_encoding
+ prefix = u'<?xml version="1.0"%s>\n' % encoding_part
+ else:
+ prefix = u''
+ return prefix + super(BeautifulSoup, self).decode(
+ pretty_print, indent_level, eventual_encoding)
+
class StopParsing(Exception):
pass