summaryrefslogtreecommitdiff
path: root/beautifulsoup/__init__.py
diff options
context:
space:
mode:
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