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
Brad007Brad007 

Apex callout and XmlStreamReader class

Hi,

 

I have been trying to perform the Apex callout . And i am stuck converting the Xml string back to a Object. And also i am receiving soem error trying to making the callout not sure if that was successful

   

I need to get the Values of string Xml in Apex and update to a object named Files. 

 

Please suggest thanks in Advance.

 

public class APImageUpdates {
  public void APFilesMissing()
    {
      string ServiceResult = '';
      System.debug('Entered!');
      
      // Create list for Available Aupairs
      List<AP_Application__c> APFiles = [Select  a.Id ,a.Au_Pair__c,a.AP_Folder_Name__c, a.Photo4__c, a.Photo3__c, a.Photo2__c, a.Photo1__c, a.IsExist_Photo4__c, a.IsExist_Photo3__c, a.IsExist_Photo2__c, a.IsExist_Photo1__c,a.Au_Pair__r.Id,a.Au_Pair__r.AP_Status__c,a.Au_Pair__r.Record_Type_Name__c  From AP_Application__c a where a.Au_Pair__r.AP_Status__c ='Available' AND a.Au_Pair__r.Record_Type_Name__c= 'Au Pair'];      
       
      // Root of the Xml list
      string XmlString = '<APFilesData>';      
                  
        for(AP_Application__c APAppObj: APFiles)
        {    
            if(APAppObj.IsExist_Photo1__c == false) 
            {  
              XmlString += '<APFiles>';
              XmlString += '<Id>';
              XmlString += APAppObj.Id;
              XmlString += '</Id>';
              XmlString += '<APID>';
              XmlString += APAppObj.Au_Pair__c;
              XmlString += '</APID>';
              XmlString += '<Filepath>';
              XmlString +=  APAppObj.Photo1__c;
              XmlString += '</Filepath>';
              XmlString += '</APFiles>';        
            }
            
            if(APAppObj.IsExist_Photo2__c == false )
            {
              XmlString += '<APFiles>';              
              XmlString += '<Id>';
              XmlString += APAppObj.Id;
              XmlString += '</Id>';
              XmlString += '<APID>';
              XmlString += APAppObj.Au_Pair__c;
              XmlString += '</APID>';
              XmlString += '<Filepath>';
              XmlString +=  APAppObj.Photo2__c;
              XmlString += '</Filepath>';
              XmlString += '</APFiles>';  
            }
            
            if(APAppObj.IsExist_Photo3__c == false )
            {
              XmlString += '<APFiles>';              
              XmlString += '<Id>';
              XmlString += APAppObj.Id;
              XmlString += '</Id>';
              XmlString += '<APID>';
              XmlString += APAppObj.Au_Pair__c;
              XmlString += '</APID>';
              XmlString += '<Filepath>';
              XmlString +=  APAppObj.Photo3__c;
              XmlString += '</Filepath>';
              XmlString += '</APFiles>';  
            }
            
            if(APAppObj.IsExist_Photo4__c == false )
            {
              XmlString += '<APFiles>';              
              XmlString += '<Id>';
              XmlString += APAppObj.Id;
              XmlString += '</Id>';
              XmlString += '<APID>';
              XmlString += APAppObj.Au_Pair__c;
              XmlString += '</APID>';
              XmlString += '<Filepath>';
              XmlString +=  APAppObj.Photo4__c;
              XmlString += '</Filepath>';
              XmlString += '</APFiles>';  
            }
      }
          //Close root Xml
          XmlString += '</APFilesData>';  
          System.debug('XML:' +XmlString);            
          System.debug('Entered1');  
          
           
            HttpRequest req = new HttpRequest();
          req.setEndpoint('http://Ip Address/Ajax.asmx');
           req.setMethod('POST');
           req.setHeader('Content-Type', 'text/xml; charset=utf-8');
           req.setHeader('SOAPAction', 'http://http://IP Address/GAPAPFiles');
          
          string b = '<?xml version="1.0" encoding="utf-8"?>';
          b += '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
          b += '<soap:Body>';
          b += '<GAPAPFiles xmlns="http://IP Address">';
          b += '<xmlfiles>' + XmlString + '</xmlfiles>';
          b += '</GAPAPFiles>';
          b += '</soap:Body>';
          b += '</soap:Envelope>';
          
          req.setBody(b);
          
          Http http = new Http();
          HTTPResponse res = http.send(req);
        
          XmlStreamReader reader = new XmlStreamReader(res.getBody());
          while(reader.hasNext()) 
          {
            if (reader.getEventType() == XmlTag.START_ELEMENT) 
            {
              string FieldName = reader.getLocalName();
              reader.next();
              if (reader.getEventType() == XmlTag.CHARACTERS) 
              {
                // how do i get the three values in to a list or some thing so that i can update them into a different object.
              }
            }
            reader.next();
          }
        
      
      }