You need to sign in to do that
Don't have an account?
Nageswara reddy
Getting XMl Tag data and Want to use in Apex class
HI all
I hava On e XML File , Now i Want Get the value of particular Tag from that XMl, Suppose MY XML file
is
<GeocodeResponse>
<status>OK</status>
</GeocodeResponse>
I want that coloured Location value , Any body can help me
Thanks
Nageswar
HI Alex,
I followed the given Code by you, It is attempting an error dereferce Null object ,
MY apex class is
----------------------
ublic class googleutil
{
public void Coordinates()
{
//string fid;
string url='http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true';
System.debug('************************'+url);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length', '0');
req.setEndpoint(url);
req.setMethod('POST');
HttpResponse res = h.send(req);
string responseBody = res.getBody().replace('\n','');
system.debug('++++++++++++++++++++++'+responseBody);
dom.Document doc= res.getBodyDocument();
dom.XmlNode rootElement = doc.getRootElement();
system.debug('+++++++++++++++'+ rootElement );
dom.XmlNode locationElement = rootElement.getChildElement( 'location',null);
system.debug('*********************'+ locationElement );
String lat =locationElement.getChildElement('lat', null).getText();
string lng = locationElement.getChildElement('lng', null).getText();
}
}
the Out put is
All Answers
It would look something like this:
HI Alex,
I followed the given Code by you, It is attempting an error dereferce Null object ,
MY apex class is
----------------------
ublic class googleutil
{
public void Coordinates()
{
//string fid;
string url='http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true';
System.debug('************************'+url);
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-type', 'application/x-www-form-urlencoded');
req.setHeader('Content-length', '0');
req.setEndpoint(url);
req.setMethod('POST');
HttpResponse res = h.send(req);
string responseBody = res.getBody().replace('\n','');
system.debug('++++++++++++++++++++++'+responseBody);
dom.Document doc= res.getBodyDocument();
dom.XmlNode rootElement = doc.getRootElement();
system.debug('+++++++++++++++'+ rootElement );
dom.XmlNode locationElement = rootElement.getChildElement( 'location',null);
system.debug('*********************'+ locationElement );
String lat =locationElement.getChildElement('lat', null).getText();
string lng = locationElement.getChildElement('lng', null).getText();
}
}
the Out put is