+ Start a Discussion
pranav shah 40pranav shah 40 

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.writeEndElement();//Element </Id>
              //Element <BookingDate>
              w.writeStartElement(null, 'BookingDate', null);
                  //w.writeStartElement(null, 'BookingDate', null);
              w.writeStartElement(null, 'Name', null);
              //Element <TradeIN>
              w.writeStartElement(null, 'TradeIN', null);
             w.writeStartElement(null, 'Type', null);
              //Element <DeliveryDate>
              w.writeStartElement(null, 'DeliveryDate', null);
              w.writeEndDocument();//this will be ROOT CLOSE of </Opportunities> in XML
         string xmlOutput = w.getXmlString();
         system.debug('XML is xmlOutput  '+xmlOutput );
         string xmldata = doc.toXmlString();
         system.debug('XML is '+xmldata);
     Blob  csvBlob;
                csvBlob  = Blob.valueOf(xmldata);  
            }catch(exception e){

                csvBlob  = Blob.valueOf('Some Text');
            return 'Reward feed already sent';
             Attachment attachment = new Attachment();
                attachment.Body = csvBlob ;
                attachment.Name = 'Rewardfile.xml';
                attachment.ParentId = rew.id;
             insert attachment;
           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

Pranav Shah