3

Can someone explain why the format of the XML looks great in the browser -

<job id="blah" name="string2" master="string3" dbversion="string4" xmlversion="string5">
    <jobmst>
        <jobmst_id>10081</jobmst_id>
        <jobmst_type>2</jobmst_type>
        <jobmst_prntid>blah blah</jobmst_prntid>
        <jobmst_active>Y</jobmst_active>
        .......

But when I save the output, or do a CURL of the page it comes out as unformatted?

<job id="blah" name="string2" master="string3" dbversion="string4" xmlversion="string5"><jobmst><jobmst_id>10081</jobmst_id><jobmst_type>2</jobmst_type><jobmst_prntid>blah blah</jobmst_prntid><jobmst_active>Y</jobmst_active>

edit - I'm learning something here. Django is formatting it to XML in the browser (good) but the source is unformatted so I need to have a separate download feature to actually format it for download?

whoisearth
  • 4,080
  • 13
  • 62
  • 130
  • Does the output in the browser have + signs prepended that you can use to click open parts of the xml tree? – flup Jan 17 '14 at 20:44
  • it does and I actually just updated my question. If I few the source it's all one line which means that django is formatting it inline on the browser. That tells me that I would need a separate view that would export to XML instead of having a user right click/save which is just saving the source hence the 1 line unformatted? – whoisearth Jan 17 '14 at 20:48

1 Answers1

2

It's perfectly fine for the XML that is served to be unformatted. This saves some bandwidth.

The browser is formatting the XML for you when it displays it. And most editors will allow you to format XML when you've opened it for viewing.

In linux/unix, you can also format XML on the command line after fetching it with curl.

I don't think that Django REST Framework allows you to pretty print XML out of the box. The JSON renderer has an indent option, but I cannot find one for XML. You could, if need be, write a custom pretty-printing XML renderer but I'd go for one of the above options instead.

Community
  • 1
  • 1
flup
  • 26,937
  • 7
  • 52
  • 74
  • aaah. libxml2 looks to be the potential solution I'm guessing I would then write this into the framework so that when a get is done to run it through libxml2 before it spits out the .xml file to the user? – whoisearth Jan 17 '14 at 21:14
  • No, the user would run xmllint on the .xml file it's fetched from your server. If you really want to serve formatted XML you'll need to write and configure a custom renderer that does that. See also http://stackoverflow.com/questions/749796/pretty-printing-xml-in-python – flup Jan 17 '14 at 21:19
  • Thanks! Consider me confused from my naivety. It makes sense now. – whoisearth Jan 18 '14 at 01:23