HTTP Callout with XML Parcing
Example 1:
goto -> administration setup -> security controls -> remote site settings.
and add
Remote Site URL : http://maps.googleapis.com
Page:
<apex:page showHeader="false" controller="XMLPraseClass" >
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:commandButton value="Click Me" action="{!clickme}" reRender="result1"/>
</apex:pageblockSection>
</apex:pageBlock>
<apex:outputPanel id="result1">
<apex:inputtextarea cols="40" rows="20" value="{!textToParse}" />
<apex:inputtextarea cols="40" rows="20" id="result" value="{!parsedText}"/>
<br />
<apex:commandButton value="Parse" action="{!parse}" reRender="result"/>
</apex:outputPanel>
</apex:form>
</apex:page>
Example 2:
step1:
Add the end point url in remote site settings.
url:
----
http://maps.googleapis.com/maps/api/geocode/xml?address=92618&sensor=false
goto -> administration setup -> security controls -> remote site settings.
step2:
write the below class:
========================
public with sharing class HttpClass {
public void clickme()
{
String strurl='http://maps.googleapis.com/maps/api/geocode/xml?address=92618&sensor=false';
HttpRequest req=new HttpRequest();
req.setEndpoint(strurl);
req.setMethod('POST'); // req.setMethod('GET');
string strbody='hi';
req.setBody(strbody);
req.setCompressed(true);
Http http=new Http();
HTTPResponse res=http.send(req);
string strRes=res.getbody();
system.debug('**** XML Data is *****'+strRes);
// Parse either XML / JSON Response using those methods (JSON / XML Parse Methods)
}
}
step3:
======
call the above class from the developer console.
Developer console code:
------------------------
HttpClass objH = new HttpClass ();
objH.clickme();
================
xml parsing:
Page:
<apex:page controller="XMLParseClass">
<apex:form >
<apex:pageblock >
<apex:pageblocksection >
<apex:inputfile value="{!objD.body}" filename="{!objD.name}"></apex:inputfile>
<apex:commandButton value="Parse" action="{!doParse}"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
class:
public with sharing class XMLParseClass {
public Document objD { get; set; }
public XMLParseClass(){
objD = new Document();
}
public void doParse(){
String xml = objD.body.toString();
Dom.Document doc = new Dom.Document();
doc.load(xml);
XMLTest__c objxml;
List<XMLTest__c> lstX = new List<XMLTest__c>();
lstX = new List<XMLTest__c>();
for(DOM.XMLNode rootnode : doc.getRootElement().getChildElements()){
objxml = new XMLTest__c();
Integer i=0;
for(DOM.XMLNode dept: rootnode.getChildElements()){
if(i==0){
objxml.Name = dept.getText();
system.debug('---objxml.name is --->'+objxml.Name);
}
if(i==1){
objxml.street1__c = dept.getText();
}
if(i==2){
objxml.street2__c = dept.getText();
}
if(i==3){
objxml.city__c = dept.getText();
}
if(i==4){
objxml.state__c = dept.getText();
}
if(i==5){
objxml.country__c = dept.getText();
}
i++;
}
lstX.add(objxml);
}
system.debug('--lstX is -->'+lstX);
insert lstX;
}
}
sample xml:
<company>
<department>
<name>Sachin</name>
<street1>808 State St</street1>
<street2>Apt. 2</street2>
<city>Palookaville</city>
<state>PA</state>
<country>USA</country>
</department>
<department>
<name>Sourav</name>
<street1>Culcutta St</street1>
<street2>Apt. 2</street2>
<city>Culcutta</city>
<state>XYZ</state>
<country>India</country>
</department>
</company>
goto -> administration setup -> security controls -> remote site settings.
and add
Remote Site URL : http://maps.googleapis.com
Controller Class for HTTP Callout and Parcing
- public with sharing class XMLPraseClass {
- public String textToParse{ get; set; }
- //clickme() is for HTTP Callout - in response it will get a XML file
- public void clickme() {
- String strurl='http://maps.googleapis.com/maps/api/geocode/xml?address=92618&sensor=false';
- HttpRequest req=new HttpRequest();
- req.setEndpoint(strurl);
- req.setMethod('POST'); // req.setMethod('GET');
- string strbody='hi';
- req.setBody(strbody);
- req.setCompressed(true);
- Http http=new Http();
- HTTPResponse res=http.send(req);
- textToParse=res.getbody();
- }
- public String parsedText {get; set;}
- // The main method for parcing the XML response received by the http callout
- public PageReference parse() {
- if (textToParse== null) {
- parsedText = 'Nothing to parse';
- } else {
- parsedText = parse(textToParse);
- }
- return null;
- }
- // Just checking that it's actually XML
- private String parse(String toParse) {
- DOM.Document doc = new DOM.Document();
- try {
- doc.load(toParse);
- DOM.XMLNode root = doc.getRootElement();
- return walkThrough(root);
- } catch (System.XMLException e) { // invalid XML
- return e.getMessage();
- }
- }
- // Recursively walk through the XML
- private String walkThrough(DOM.XMLNode node) {
- String result = '\n';
- if (node.getNodeType() == DOM.XMLNodeType.COMMENT) {
- return 'Comment (' + node.getText() + ')';
- }
- if (node.getNodeType() == DOM.XMLNodeType.TEXT) {
- return 'Text (' + node.getText() + ')';
- }
- if (node.getNodeType() == DOM.XMLNodeType.ELEMENT) {
- result += 'Element: ' + node.getName();
- if (node.getText().trim() != '') {
- result += ', text=' + node.getText().trim();
- }
- if (node.getAttributeCount() > 0) {
- for (Integer i = 0; i< node.getAttributeCount(); i++ ) {
- result += ', attribute #' + i + ':' + node.getAttributeKeyAt(i) + '=' + node.getAttributeValue(node.getAttributeKeyAt(i), node.getAttributeKeyNsAt(i));
- }
- }
- for (Dom.XMLNode child: node.getChildElements()) {
- result += walkThrough(child);
- }
- return result;
- }
- return ''; //should never reach here
- }
- }
Page:
<apex:page showHeader="false" controller="XMLPraseClass" >
<apex:form >
<apex:pageBlock >
<apex:pageblockSection >
<apex:commandButton value="Click Me" action="{!clickme}" reRender="result1"/>
</apex:pageblockSection>
</apex:pageBlock>
<apex:outputPanel id="result1">
<apex:inputtextarea cols="40" rows="20" value="{!textToParse}" />
<apex:inputtextarea cols="40" rows="20" id="result" value="{!parsedText}"/>
<br />
<apex:commandButton value="Parse" action="{!parse}" reRender="result"/>
</apex:outputPanel>
</apex:form>
</apex:page>
Example 2:
step1:
Add the end point url in remote site settings.
url:
----
http://maps.googleapis.com/maps/api/geocode/xml?address=92618&sensor=false
goto -> administration setup -> security controls -> remote site settings.
step2:
write the below class:
========================
public with sharing class HttpClass {
public void clickme()
{
String strurl='http://maps.googleapis.com/maps/api/geocode/xml?address=92618&sensor=false';
HttpRequest req=new HttpRequest();
req.setEndpoint(strurl);
req.setMethod('POST'); // req.setMethod('GET');
string strbody='hi';
req.setBody(strbody);
req.setCompressed(true);
Http http=new Http();
HTTPResponse res=http.send(req);
string strRes=res.getbody();
system.debug('**** XML Data is *****'+strRes);
// Parse either XML / JSON Response using those methods (JSON / XML Parse Methods)
}
}
step3:
======
call the above class from the developer console.
Developer console code:
------------------------
HttpClass objH = new HttpClass ();
objH.clickme();
================
xml parsing:
Page:
<apex:page controller="XMLParseClass">
<apex:form >
<apex:pageblock >
<apex:pageblocksection >
<apex:inputfile value="{!objD.body}" filename="{!objD.name}"></apex:inputfile>
<apex:commandButton value="Parse" action="{!doParse}"/>
</apex:pageblocksection>
</apex:pageblock>
</apex:form>
</apex:page>
class:
public with sharing class XMLParseClass {
public Document objD { get; set; }
public XMLParseClass(){
objD = new Document();
}
public void doParse(){
String xml = objD.body.toString();
Dom.Document doc = new Dom.Document();
doc.load(xml);
XMLTest__c objxml;
List<XMLTest__c> lstX = new List<XMLTest__c>();
lstX = new List<XMLTest__c>();
for(DOM.XMLNode rootnode : doc.getRootElement().getChildElements()){
objxml = new XMLTest__c();
Integer i=0;
for(DOM.XMLNode dept: rootnode.getChildElements()){
if(i==0){
objxml.Name = dept.getText();
system.debug('---objxml.name is --->'+objxml.Name);
}
if(i==1){
objxml.street1__c = dept.getText();
}
if(i==2){
objxml.street2__c = dept.getText();
}
if(i==3){
objxml.city__c = dept.getText();
}
if(i==4){
objxml.state__c = dept.getText();
}
if(i==5){
objxml.country__c = dept.getText();
}
i++;
}
lstX.add(objxml);
}
system.debug('--lstX is -->'+lstX);
insert lstX;
}
}
sample xml:
<company>
<department>
<name>Sachin</name>
<street1>808 State St</street1>
<street2>Apt. 2</street2>
<city>Palookaville</city>
<state>PA</state>
<country>USA</country>
</department>
<department>
<name>Sourav</name>
<street1>Culcutta St</street1>
<street2>Apt. 2</street2>
<city>Culcutta</city>
<state>XYZ</state>
<country>India</country>
</department>
</company>
Comments
Post a Comment