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
bdbagleybdbagley 

XMLStreamReader or XML dom assistance

As I've seen on here a couple times, I'm trying to do an HTTP request and grab the xml data returned from google.  However, I've hit a wall while reading up on these things (I've been teaching myself coding for about 3 weeks now so sorry if this is a simple matter).  I think I have my HTTP request working and I'm attempting to grab the entire XML document and list it in a text box while also specifically grabbing the Longitude and Lattitude.  My overall goal is to type in a city and state but I want to just get it working before I dive into that (that's why it is commented out).  Honestly I think I'm on the right track buy having difficulty transcribing the info into a visualforce page.  Here's my attempted code:

 

public class interview6 {

    public String city {get;set;}
    public String state {get;set;}
    
public void Coordinates()
{
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            String url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=Kennesaw,+GA&sensor=false';
            // generate the url for the request
            //String url = 'http://maps.google.com/maps/api/geocode/xml?address='+ EncodingUtil.urlEncode(city,'UTF-8')+',+'
                //+ EncodingUtil.urlEncode(state,'UTF-8')
               // +'&sensor=false';
            // set the method
            req.setMethod('GET');
            // add the endpoint to the request
            req.setEndpoint(url);
HttpResponse res = http.send(req);
string responseBody = res.getBody().replace('\n','');
 string longitude;
 string latitude;
dom.Document doc = res.getBodyDocument();
//geometry
dom.XmlNode location = doc.getRootElement().getChildElement('result',null).getChildElement('geometry',null).getChildElement('location',null);         

longitude = location.getChildElement('lng', null).getText();
latitude = location.getChildElement('lat', null).getText();

    }

}

 

<apex:page controller="interview6">
  <apex:sectionHeader title="Interview Six"/>
    <apex:form >
        <apex:pageBlock >
          <apex:pageBlockSection title="Input" Columns="1">
            <apex:pageBlockSectionItem >
                  <apex:outputLabel for="City">City</apex:outputLabel>
               <apex:panelGroup >
                  <apex:inputText id="City" value="{!city}"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                 <apex:outputLabel for="State">State</apex:outputLabel>
               <apex:panelGroup >
                 <apex:inputText id="State" value="{!State}"/>
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
         </apex:pageBlockSection>
         
         <apex:pageblockSection title="Output" Columns="1">
            <apex:pageBlockSectionItem >
                  <apex:outputLabel for="HTTP Response">HTTP Response</apex:outputLabel>
               <apex:panelGroup >
                  <apex:InputTextarea Cols="40" rows="20" />
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                  <apex:outputLabel for="Lattitude">Lattitude</apex:outputLabel>
               <apex:panelGroup >
                  <apex:InputText id="Lattitude" />
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
            <apex:pageBlockSectionItem >
                 <apex:outputLabel for="Longitude">Longitude</apex:outputLabel>
               <apex:panelGroup >
                 <apex:InputText id="Longitude" />
               </apex:panelGroup>
            </apex:pageBlockSectionItem>
         </apex:pageBlockSection>                 
       </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Again I'm sure there are some issues with the above sample, but if I could just get some help with displaying the entire code in the text box I feel I could work from there without much issue.  Thanks for taking the time to look.

Best Answer chosen by Admin (Salesforce Developers) 
bdbagleybdbagley

Last update, finally got it working so I'll update this page for anyone else looking for similar information.

 

    public void Coordinates()
    {
        try
        {
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            // generate the url for the request
            String url = 'http://maps.googleapis.com/maps/api/geocode/xml?address='+ EncodingUtil.urlEncode(city,'UTF-8')+',+'
                + EncodingUtil.urlEncode(state,'UTF-8')
                +'&sensor=false';
            // add the endpoint to the request
            req.setEndpoint(url);
            // set the method
            req.setMethod('GET');
            HttpResponse res = http.send(req);
            dom.Document doc = res.getBodyDocument();
            responseBody = res.getBody().replace('\n', '');
            
            //get elements
            dom.XmlNode location = doc.getRootElement().getChildElement('result',null).getChildElement('geometry',null).getChildElement('location',null);         
            longitude = location.getChildElement('lng', null).getText();
            latitude = location.getChildElement('lat', null).getText();
            
        }   

 I made a couple adjustments to the visualforce page, but they are fairly basic so one should be able to work through it.

All Answers

bdbagleybdbagley

Last update, finally got it working so I'll update this page for anyone else looking for similar information.

 

    public void Coordinates()
    {
        try
        {
            Http http = new Http();
            HttpRequest req = new HttpRequest();
            // generate the url for the request
            String url = 'http://maps.googleapis.com/maps/api/geocode/xml?address='+ EncodingUtil.urlEncode(city,'UTF-8')+',+'
                + EncodingUtil.urlEncode(state,'UTF-8')
                +'&sensor=false';
            // add the endpoint to the request
            req.setEndpoint(url);
            // set the method
            req.setMethod('GET');
            HttpResponse res = http.send(req);
            dom.Document doc = res.getBodyDocument();
            responseBody = res.getBody().replace('\n', '');
            
            //get elements
            dom.XmlNode location = doc.getRootElement().getChildElement('result',null).getChildElement('geometry',null).getChildElement('location',null);         
            longitude = location.getChildElement('lng', null).getText();
            latitude = location.getChildElement('lat', null).getText();
            
        }   

 I made a couple adjustments to the visualforce page, but they are fairly basic so one should be able to work through it.

This was selected as the best answer
mamaa mythia 2mamaa mythia 2
Hi bdbagley,

I was attracted to the subject as raised. "XMLStreamReader or XML dom assistance".
Any reasons or reference for using XML Dom assistance over XMLStreamReader? Factoring in Performance and complexity. 
Much appreciated for your any sharing.
Thanks!

Best Regards,
sfdc webservicessfdc webservices
Hi bdbagley,
I have tried above code what you posted I am getting below error like


13:104 no viable alternative at character '"'  

String url = '<a rel="nofollow" href="http://maps.googleapis.com/maps/api/geocode/xml?address='+" target="_blank">http://maps.googleapis.com/maps/api/geocode/xml?address='+</a> EncodingUtil.urlEncode(city,'UTF-8')+',+'
            + EncodingUtil.urlEncode(state,'UTF-8')
            +'&sensor=false';

If possible please send me correct code to my mail
malli.cseasra@gmail.com

Thanks,