2

I have the following code in my controller

public PageReference generateDocument()
           {
            XmlStreamWriter writer = new XmlStreamWriter();

            writer.writeStartDocument('utf-8', '1.0');        
            writer.writeStartElement(null, 'Liasse', null);

            writer.writeStartElement(null, 'Modele', null);
            writer.writeStartElement(null, 'Id', null);
            writer.writeCharacters('7');

            writer.writeEndElement();
            writer.writeEndElement();
            writer.writeEndElement();
            writer.writeEndDocument();


           PageReference pr = Page.XML;
           xml = writer.getXmlString();

           pr.setRedirect(false);
           return pr;

           }
public static String xml{get;set;}

I want to display an xml file by using the following page

<apex:page contentType="html/xml#test.xml" controller="ActifController">
<apex:outputText value="{!xml}" />

</apex:page>

It is working, except the file that I get is not well-indentated. Because it's just a String obviously. Is there any way I can format the string or something so that the file looks like a proper xml file?

EDIT: When viewing the file on notepad++ there is no indentation, but viewing the file using Internet Explorer, it looks fine

4

There doesn't appear to be any way to do this on the platform out of the box, I think you'd have to build up the string yourself taking care of the indentation in code which would frankly be messy and a pain to maintain.

The spaces and indentation are just for us mere humans, no software is going to require it to understand the XML, hence why you're seeing IE show the file with nice indentation for your benefit. The same happens in Firefox if I set a Visualforce's content type to "text/xml".

In reality not having the indentation saves bandwidth, so you're better off without it. If you really need it for some reason you're probably better finding a tool to pipe it through on the receving end.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.