4

Currently, I have been trying to generate XML in the simplest way possible. So far I have almost everything except for one minute detail.

Currently, I am trying to print out the following:

<?xml version="1.0" encoding="UTF-8" ?>

However, whenever I try that, it blows up the page with this: The XML bombing

Currently, I have only seen two options, actually three if I just leave it out entirely.

The first option is to change the API version of the page to 19. I got the whole idea for generating my XML via a VisualForce page here. The only problem is, I am using dynamic references to access the fields on my custom object (the main reason I chose this route in the first place). So this is a no-go for me.

The second, which used to work I believe, is to use a non-escaped output tag.

Basically, this:

<apex:page standardController="Example__c" extensions="ExampleResponseController" 
    recordSetVar="Examples"
    contentType="text/xml" showHeader="false" sidebar="false" cache="false">
    <apex:outputText value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;" escape="false"/>
    <response>
        <apex:repeat value="{!ExamplesToHaveXML}" var="Example">
            <apex:outputText value="&lt;Example id=&quot;{!Example.Id}&quot;&gt;" escape="false"/> 
                <apex:repeat value="{!ExampleFields}" var="Field">
                    <apex:outputText value="&lt;{!Field}&gt;" escape="false"/> 
                        <apex:outputText value="{!Example[Field]}"/>
                    <apex:outputText value="&lt;/{!Field}&gt;" escape="false"/> 
                </apex:repeat>
            <apex:outputText value="&lt;/Example&gt;" escape="false"/> 
        </apex:repeat>
    </response>
</apex:page>

This works great for generating all the other XML tags, but not the most important one. I check to see if it rendered properly in HTML - which it did - but, when I change the content type of the page to XML, it blows up.

I don't know if I can forgo this tag altogether because the rest of it actually works. I just have no idea why of all things, this has to be the issue. Any thoughts?

2 Answers 2

1

I got it!

After some careful tinkering, I actually got my XML to output properly.

I was able to use my controller extension to format my XML header information.

Here's how I did it:

Within my Controller Extension

public String XMLHeadingInformation { get; set; }
...
XMLHeadingInformation = '<?xml version="1.0" encoding="UTF-8" ?>';

Since I knew I could easily escape the double quotes on the controller side, I figured I could just render it as an output tag.

The VisualForce to Render

<apex:page standardController="Example__c" extensions="ExampleResponseController" 
    recordSetVar="Examples"
    contentType="text/xml" showHeader="false" sidebar="false" cache="false" >
    <apex:outputText value="{!XMLHeadingInformation}" escape="false"/>
    <response>
        <apex:repeat value="{!ExamplesToHaveXML}" var="Example">
            <apex:outputText value="<Example>" escape="false"/>
                <apex:repeat value="{!ExampleFields}" var="Field">
                    <apex:outputText value="<{!Field}>" escape="false"/> 
                        <apex:outputText value="{!Example[Field]}"/>
                    <apex:outputText value="</{!Field}>" escape="false"/>
                </apex:repeat>
            <apex:outputText value="</Example>" escape="false"/> 
        </apex:repeat>
    </response>
</apex:page>

As you can see, I change it a little bit and it actually renders everything properly. Aside from some formatting I have to do to the object before hand, it works great.

0

Its not blowing your page up. Its ur browser that's showing you the error. Browser does some preprocessing before showing you the data and thats what making it break.

On your error page, if you right click and view source of page.. you will get the intact XML.

1
  • This didn't work for me. It never showed my my XML even when I requested the page source. I managed to correct it though. Sep 1, 2016 at 19:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .