function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
naresh.sfdc1.3885733215764783E12naresh.sfdc1.3885733215764783E12 

Save XML File in Attachments

Hi Folks,

i implemented one VF Page for showing Data in XML format.Its showing very perfetly but when i click on Save button on this page,i need to save this Data in Attachments as an XML file.Any ideas....

VF Page:

<apex:page showHeader="false" controller="CLS_xmlGeneration" tabStyle="Reward__c" >
                <apex:outputText value="{!xmldata}" style="width:1000px;"/>
</apex:page>

Class :
public class CLS_xmlGeneration {

    public String xmldata { get; set; }
 
    public ID recid{get;set;}
 
    public Reward__c rew{get;set;}
 

 
    Public CLS_xmlGeneration(){
        recid=ApexPages.CurrentPage().getParameters().get('Id');

        rew =new Reward__c();
        rew = [select id,name,Rward_Amount__c,Time_Based_Mgmnt__c,User__c from Reward__c where id=:recid];

        Dom.Document doc = new Dom.Document();
        XmlStreamWriter w = new XmlStreamWriter();
        w.writeStartDocument(null, '1.0');
       // w.writeStartElement(null, 'quotas', null);
    
              w.writeStartElement(null, 'Reward', null);
        
              w.writeStartElement(null, 'Id', null);
              w.writeCharacters(rew.id);
              w.writeEndElement();
    
              w.writeStartElement(null, 'Name', null);
              w.writeCharacters(rew.name);
              w.writeEndElement();

              w.writeStartElement(null, 'UserId', null);
              w.writeCharacters(rew.User__c);
              w.writeEndElement();

              w.writeStartElement(null, 'TimeId', null);
              w.writeCharacters(rew.Time_Based_Mgmnt__c);
              w.writeEndElement();
    
              w.writeStartElement(null, 'RewardAmount', null);
              w.writeCharacters(string.valueof(rew.Rward_Amount__c));
              w.writeEndElement();
    
              w.writeEndElement();
    
       //  w.writeEndElement();
        w.writeEndDocument();
       xmldata  = w.getXmlString();
          w.close();
       // doc.LoadXml(xmlOutput);
       // system.debug('XML is '+doc.toXmlString());
       // xmldata = doc.toXmlString();

    }
    public pagereference insrtattacmnt(){
         PageReference csvPage = Page.VF_XMLinsert_intoAttachment;
         csvPage.getParameters().put('Id', recid);
         Blob csvBlob = csvPage.getContent();
      
       //    dom.Document doc = new dom.Document();
       //    doc.load(csvPage.getContent().toString());
     
         Attachment attachment = new Attachment();
            attachment.Body = csvBlob ;
            attachment.Name = 'Rewardfile.xml';
            attachment.ParentId = rew.Id;
          insert attachment;

       
      return null;
    }
}
Offshore Freelance ConsultantOffshore Freelance Consultant
Hi,

The reason the xml is not coming up right when it is attached to the object/attachment is because, you are using the same visual force page where you are not specifying any content type in the <apex:page tag.

if you specify content type as xml in 
<apex:page showHeader="false" controller="CLS_xmlGeneration" tabStyle="Reward__c"  contentType="xml"> in the same vf page, that will mess up the vf page and hence it will not show the button to make an attachment etc.

So the workaround I found is as below.

I have the controller as below.(modified queries for my dev org purposes)

---------------
public class CLS_xmlGeneration {
    public String xmldata { get; set; }

    public ID recid{get;set;}

    public account rew{get;set;}

    Public CLS_xmlGeneration(){
        recid=ApexPages.CurrentPage().getParameters().get('Id');

        rew =new account();
        rew = [select id,name from Account where id=:recid];
        Dom.Document doc = new Dom.Document();
        XmlStreamWriter w = new XmlStreamWriter();
       
        w.writeStartDocument(null, '1.0');
        w.writeStartElement(null, 'Reward', null);
        w.writeStartElement(null, 'Id', null);
        w.writeCharacters(rew.id);
        w.writeEndElement();
   
        w.writeStartElement(null, 'Name', null);
        w.writeCharacters(rew.name);
        w.writeEndElement();

   
        w.writeEndDocument();
        xmldata  = w.getXmlString();
        w.close();
    }
   
    public pagereference insrtattacmnt(){
    
         Attachment attachment = new Attachment();
        
         PageReference xmlf= Page.XMLFile;
         xmlf.getParameters().put('Id', recid);
         Blob xmlBlob= xmlf.getContent();
                  attachment.Body = xmlBlob;
            attachment.Body =xmlBlob;
            attachment.Name = 'Rewardfile.xml';
            attachment.ParentId = rew.Id;
            attachment.ContentType = 'application/octet-stream';
          insert attachment;

      
      return null;
    }
}

--------------------
Then have two VF pages, one for button purpose(UI) and other for attachment purpose with content type set.

-----------------
VF_XMLinsert_intoAttachment

<apex:page showHeader="false" controller="CLS_xmlGeneration"  >
<apex:message/>
                <apex:form>
                     <apex:outputText value="{!xmldata}" style="width:1000px;"/>
                     <br/>
                    <apex:commandButton action="{!insrtattacmnt}" value="save"/>
                </apex:form>   
</apex:page>

----------------------
XMLFile  - second vf page

<apex:page showHeader="false" controller="CLS_xmlGeneration"  contentType="xml">
<apex:outputText value="{!xmldata}" />
</apex:page>

-----------------------

********************************
Hope this helps!

JG
--------------------------------
Please motivate me for spending my Time, Effort & Skills in solving your problems by Your 'Solved!', Kudos and 'Like!'