0

I have a string like this :

my_xml = '''
        <?xml version="1.0" encoding="utf-8" ?> \n
        <entries> \n
        <entry>1</entry> \n 
        <entry>2</entry> \n
        </entries>
    '''
return HttpResponse(my_xml)

This is my output it was printed without new line and without xml tags:

1 2 

How would we add a newline in Python and make browser interpreter xml tags and print them?

Drwhite
  • 1,545
  • 4
  • 21
  • 44
  • Since Martijn Pieters didn't cover this aspect in his answer: `\n` is how to write a newline character in Python. That is, what you've written is correct, -if- a newline character is actually what you need in those locations. – kampu Jun 05 '13 at 12:47
  • 3
    @kampu: the triple-quoting *also* adds newlines. The `s` sample string has, in total, 4 newline characters in it. – Martijn Pieters Jun 05 '13 at 12:48
  • could this be that `s` is correct and has correct number of new lines but as soon as it returned as `HttpResponse(s)` it displayed incorrectly. – oleg Jun 05 '13 at 13:05
  • @oleg: exactly; the browser is displaying this as HTML.. – Martijn Pieters Jun 05 '13 at 13:06

1 Answers1

8

You are looking at the output in a browser; HTML browsers consider newlines whitespace, collapsing successive whitespace characters to form one space.

Your browser is interpreting the response as HTML-formatted data, use <br/> tags instead:

s = """ this is line 1<br/>
    this is line 2<br/>
    and other lines ..."""

If you expected to see just newlines, look at the response source code instead; the newlines are there.

If you wanted to see XML output in a browser, you should set a content type header (text/xml) so the browser knows you are sending XML instead of HTML:

return HttpResponse(s, content_type='text/xml')  # Assumes you are using Django

Your browser will use a default stylesheet to display XML data (usually as a tree with collapsible sections). You can use a XML stylesheet (XSLT) to override that behaviour. Add a stylesheet header:

<?xml-stylesheet type="text/xsl" href="script.xsl" ?>

the browser will fetch the named stylesheet and apply it to your XML.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • The `
    ` works pefectly.But if my lines are HTML tags, it will not display them, it print just newline
    – Drwhite Jun 05 '13 at 12:47
  • 1
    @Drwhite: sorry, I cannot seem to parse what you are trying to say there. – Martijn Pieters Jun 05 '13 at 12:48
  • 1
    @Drwhite: That is what the browser *does*; it interprets HTML tags to form formatted text. `
    ` is the HTML tag for a line break.
    – Martijn Pieters Jun 05 '13 at 12:49
  • For the record: it's simply `
    ` in HTML.
    – kirelagin Jun 05 '13 at 12:51
  • 2
    @kirelagin: XHTML habits die hard. Either is fine for straight HTML. – Martijn Pieters Jun 05 '13 at 12:52
  • 1
    Err... ... do you mean you want to display the HTML tags literally? You will need to HTML-escape your string.http://stackoverflow.com/questions/1061697/whats-the-easiest-way-to-escape-html-in-python – Chris Barrett Jun 05 '13 at 12:53
  • Actually, my string is like this: `s = '

    ...
    '` but it print just newlines :S
    – Drwhite Jun 05 '13 at 12:53
  • 1
    @Drwhite: You want to set a content type header (`text/xml`), and a browser displaying XML generally will format it as a tree. You have problem with how the browser displays your output, not with Python newlines at all. – Martijn Pieters Jun 05 '13 at 12:54
  • @MartijnPieters Well, strictly speaking, no, there is no such thing as “self-closing tag” in HTML. Tags are either “normal” or void, so, technically, it is not valid HTML5 (I'm not sure about previous versions though). – kirelagin Jun 05 '13 at 12:55
  • @ChrisBarrett: It looks like the OP is trying to format XML data with newlines; the browser will display XML any way it pleases unless you include a stylesheet. – Martijn Pieters Jun 05 '13 at 12:55
  • @ChrisBarrett: it seems working if i replace `<` to `<` and `>` to `>` – Drwhite Jun 05 '13 at 12:59
  • what framework do you use? in jinja template system you can mark as safe using, well the "safe" tag! I've forgot the same for the django classic templates ... Also in django you have the "mark_safe" utility (look at the [code](https://github.com/django/django/blob/master/django/utils/safestring.py) there) ... Anyway hope it helps a bit, because this question wasnt tagged for anything but "python" and "string" – StefanNch Jun 05 '13 at 13:05
  • @Drwhite: Yes, escaping the <> will make things 'appear correct' in most cases. But, the correct way to do this, that will give the most 'proper' display, is to send the right Content-Type (`text/xml`, as Martijn says) header with your response. – kampu Jun 05 '13 at 13:09
  • @MartijnPieters: i have reformulate the question to point to the aim :) – Drwhite Jun 05 '13 at 13:23
  • @Drwhite: set the content type. – Martijn Pieters Jun 05 '13 at 13:24
  • @MartijnPieters: It display nothing, and when i click `CTRL+U` the source code is there ! – Drwhite Jun 05 '13 at 13:30