+ Start a Discussion
Syed Subhan 9Syed Subhan 9 

how to read and update the values of xml file by apex by storing xml file in static resource or custom object. Help will be very much appreciated.

Marcelo CostaMarcelo Costa
Sorry Syed,
But I think we will need more details to help you...
Syed Subhan 9Syed Subhan 9
Hi Marcelo. I need to store xml file in static resource more like template and in that i keep itags like tis %%Account.AccountName@@, %%Account.Owner@@ , and second section would be related object info like tis  %%Contact.ContactFirstName@@ %%Contact.ContactLastName@@
Moreover I need to pass the those itags data dynamically into the template stored in the static resource.
If Account has more than 2 contacts we need to dynamically fetch the data and put it in template.  

How do I achieve this. thanks.

My code is :

public class staticgetdata {
    
    public staticgetdata(){
        String str='';
        String objectName='';
        String iitags='';
        set<String> accountIteg = new set<String>();
         Map<String,set<string>> accMap = new Map<String,set<String>>();
        Map<String,String> oppMap = new Map<String,String>();
        Map<String,String> consultantMap = new Map<String,String>();
        Map<String,String> contactmap = new Map<String,String>();
        
        StaticResource srObject = [select id,body,BodyLength from StaticResource Where Name = 'accountfirst'];
        Integer count = srObject.BodyLength;
        
        String contents = srObject.body.toString();
        
        String regex =  '[%%]+[a-zA-Z0-9_.]*[@@]+';
        Pattern regexPattern = Pattern.compile(regex);
        Matcher regexMatcher = regexPattern.matcher(contents);
        system.debug('regexMatcher@@@'+regexMatcher);
        while (regexMatcher.find())
        {
            String itag=regexMatcher.group();
           
                itag = itag.replaceAll('@@','');

                itag = itag.replaceAll('%%','');
            system.debug('itag@@'+itag);
         
             String[] tokens=itag.split('\\.');
            system.debug('xxx'+tokens.size());
      for (integer i = 0; i<tokens.size(); i++){
      
          if(i==0){
            objectName=tokens[i];  
          }
          else if(i==tokens.size()-1){
              iitags=tokens[i];
          }
          
         system.debug('objectName@@ '+objectName);
          system.debug('iitags@@ '+iitags);
          if(objectName=='Account'){
              system.debug('objectNameCCC'+objectName);
              //accMap.put(objectName, iitags);
              accountIteg.add(iitags);
               system.debug('accMap@@@1'+accMap); 
          }
          
           if(objectName=='Opportunity'){
             
              oppMap.put(objectName, iitags);
               system.debug('oppMap@@@1'+oppMap); 
          }
                         if(objectName=='Consultant'){
             
              consultantMap.put(objectName, iitags);
                       system.debug('consultantMap@@@1'+consultantMap); 
          }
           if(objectName=='Contact'){
             
              contactmap.put(objectName, iitags);
               system.debug('contactmap@@@1'+contactmap); 
          }
              
         
      }

         
           
        }
       accMap.put('Account', accountIteg) ;
        string addFilds='';
        for(String temp:accMap.get('Account')){
            if(!String.isEmpty(temp)){
                addFilds=addFilds+','+temp;
            system.debug('addFildsxxx '+addFilds);
            }
            
           
        }
        
       
        system.debug('accMappp>>'+accMap);
        set<String> accString = accMap.get('Account');
        System.debug('accString'+accString);
       String queryAccount = 'Select Id'+ addFilds+'From Account';
      System.debug('queryAccountXXXX'+queryAccount);