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
pranavshahpranavshah 

Creation & Generation of file in XML format

Hi All,

Have any worked on creation of XML file through apex class using blob function & sending the file through batch class.


1.I have written a class which gives me O/P in XML format.
2.Now i want to create and generate an file with .XML file format.
3.Sending the file through batch class to an Email Address.


I have wriiten class
global class cls_OppFilessent
    {

     public string xmlstring { get; set;}
     public static string OppFilesend()
     {
       //Opportunity rew=new Opportunity();
        List<opportunity> lstopt =[SELECT id,Name,Booking_Date__c,Type,Trade_In__c,VW_Delivery_Date__c FROM Opportunity Limit 2];

    if (lstopt != null)
    {
    system.debug('Hariom Ganesha : listOpty='+lstopt.size());
    }
        
      Dom.Document doc = new Dom.Document();
      XmlStreamWriter w = new XmlStreamWriter();
      w.writeStartDocument(null, '1.0');
      w.writeStartElement(null, 'Opportunities', null); //this will be ROOT of <Opportunities> in XML


              for(Opportunity rew:lstopt)
              {
              w.writeStartElement(null, 'Opportunity', null);//Element <Opportunity>
          
              w.writeStartElement(null, 'Id', null);//Element </Id>
              w.writeCharacters(rew.Id);
              w.writeEndElement();//Element </Id>
      
              //Element <BookingDate>
              w.writeStartElement(null, 'BookingDate', null);
              //w.writeEndElement();
              if(rew.Booking_Date__c!=null)
              {
                  //w.writeStartElement(null, 'BookingDate', null);
                  w.writeCharacters(String.valueOf(rew.Booking_Date__c)); 
                     
              }
              w.writeEndElement();
              
              
              w.writeStartElement(null, 'Name', null);
              if(rew.Name!=null)
              {
                  w.writeCharacters(rew.Name);
                  
              }
                  w.writeEndElement();
                  
              
              
              //Element <TradeIN>
              w.writeStartElement(null, 'TradeIN', null);
              if(rew.Trade_In__c!=null)
              {
                  w.writeCharacters(String.valueOf(rew.Trade_In__c));
                  
              }
                  w.writeEndElement();
                  
                  
             w.writeStartElement(null, 'Type', null);
              if(rew.Type!=null)
              {
                  w.writeCharacters(rew.Type);
              }
                  w.writeEndElement();
              
              //Element <DeliveryDate>
              w.writeStartElement(null, 'DeliveryDate', null);
              if(rew.VW_Delivery_Date__c!=null)
              {
                  w.writeCharacters(String.valueOf(rew.VW_Delivery_Date__c)); 
              }
                  w.writeEndElement();
              }
               w.writeEndElement();
              w.writeEndDocument();//this will be ROOT CLOSE of </Opportunities> in XML
         string xmlOutput = w.getXmlString();
         system.debug('XML is xmlOutput  '+xmlOutput );
         w.close();
         doc.Load(xmlOutput);
         string xmldata = doc.toXmlString();
         system.debug('XML is '+xmldata);
   
   
    /*
     Blob  csvBlob;
     try{
                csvBlob  = Blob.valueOf(xmldata);  
            }catch(exception e){

                csvBlob  = Blob.valueOf('Some Text');
           }
     
        if(rew.Active__c==true){
            return 'Reward feed already sent';
        }else{
             Attachment attachment = new Attachment();
                attachment.Body = csvBlob ;
                attachment.Name = 'Rewardfile.xml';
                attachment.ParentId = rew.id;
             insert attachment;
          
           rew.Active__c=true;
           Update rew; 
          return 'XML: '+xmldata;
         }*/
         
         
         //String myString = 'StringToBlob';
         //Blob myBlob = Blob.valueof(myString);
         
         return 'XML: '+xmldata; // Pranav - temporary return - remove this once BLOB code is tested
    
   }
}

Now i want to create and generate an .XMLfile  so that it can be send through email

please suggest


Regards
Pranav Shah