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
LaxmanLaxman 

Calling external webservice from visual force page

I can't import the external webservice wsdl to salesforce.com as the WSDL is not supported.

So finally I have to call webservice  by sending the XML soap massage with Http.send().

 

There is a requirement to call the external webservice from Visual force page and need to

populate the debatable /grid with the response of the webservice.

 

 

Can any body help me out to parse the webservice response and populate the same in a

data table of visual force page.

 

Any help will be appreciated.

 

Thanks in advance ...

aalbertaalbert

You are on the right track. The easiest way to map the web service response into a visualforce page is to define another Apex Class that represents your external data structure (being returned in the web service). So then you can iterate through your web service response, build a list of the apex class objects and lastly, build a dataTable or grid in Visualforce - passing it the List of apex classes.

 

Apex supports XMLStreamReader classes to help with XML parsing:  XMLStreamReaderClass

 

laxdevlaxdev

Thanks aalbert  . Thanks a lot for your help. Really it help me a lot .

When i tried the sample code of Books , its working for me .

Can you please help me out for parsing the webservice response attached below.

 

Below is my webservice response and the code which I have tried is attached below.

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body><add:addressValidationResponse xmlns:add="
http://abc.com/AddressValidation">
<ns:AddressValidationResponse resultstring="Success" xmlns:ns="
http://abc.com/AddressValidation/AddressValidationResponse/01">
  <success>
    <Code>ERR_0007</Code>
    <Message>Multiple State,city,county combination exist.Refer the addresses returned</Message>
    <AddressList>
      <Address>
        <city>EAST WINDSOR</city>
        <county>MERCER</county>
        <state>NJ</state>
        <zip>
          <zip5>08520</zip5>
        </zip>
      </Address>
      <Address>
        <city>HIGHTSTOWN</city>
        <county>MERCER</county>
        <state>NJ</state>
        <zip>
          <zip5>08520</zip5>
        </zip>
      </Address>
    </AddressList>
  </success>
</ns:AddressValidationResponse>
</add:addressValidationResponse>
</soapenv:Body>
</soapenv:Envelope>

 

Below is my apex class which is the custom controller for my visual force page , I am facing the problem in parsing :

 

public class parseWebserviceRes{

        public class Address
        {
                 String City;
                 String County;
                 String State;
                 String Zip;
        
}
        
     Public Address[] getAddress()
    {   
         //calling the webservice below and the xml response bieng stored in XMLstr
        String XMlstr= MyTestWebservice.AddressValidation();
        System.debug(str);
        XmlStreamReader reader = new XmlStreamReader(XMlstr);
        Address[] addresses= parseAddresses(reader);
        System.debug('size of Address object:'+addresses.size());

             for (Address address : Addresses) {
               System.debug(address );
             }

          return addresses;
    
     }
    
     public Address[] parseAddresses(XmlStreamReader reader)
     {
         Address[] addresses= new Address[0];
          While(reader.hasNext())
         {
              
              if (reader.getEventType()== XmlTag.START_ELEMENT )
              {
                   if (reader.getLocalName()=='Address')
                   {
                      Address address= parseAddress(reader);
                      addresses.add(address);

                   }
              }
                         
              reader.next();

         }    
           
           return  addresses;
     }
    
    
    Address parseAddress(XmlStreamReader reader)
    {
     Address  address  = new Address();
      while(reader.hasNext()) {
        
        if (reader.getEventType() == XmlTag.END_ELEMENT) {
           System.debug('we are at endelement get text  ');
           break;
          

        } else if(reader.getLocalName()=='city'){
                    reader.next();
              if (reader.getEventType() == XmlTag.CHARACTERS) {
                   address.City = reader.getText();                 
            }
           
        }else if(reader.getLocalName()=='county'){
                reader.next();
            if (reader.getEventType() == XmlTag.CHARACTERS) {
                   address.County = reader.getText();
            }
           
        }else if(reader.getLocalName()=='state'){
                reader.next();
            if (reader.getEventType() == XmlTag.CHARACTERS) {
                   address.State= reader.getText();
             }
        }
            
      


        reader.next();
     }
     return address  ;
   }


}

laxdevlaxdev

Thanks aalbert . Thanks a lot .

I am very new to this kind of parsing. Can you please help me out . I have added the details of my XML and the apex code .

 

Thanks....