Hi Guys,
Few days back i was working on an integration of Salesforce
with Zoho,
in that integration i am calling web services of Zoho from
Salesforce by using REST API's,
i am getting response in form of XML from ZOHO and it takes
my couples of hours only for
parsing(reading) that XML.
So in this post i am showing you how can we read an XML
easily and take-out our
required data from XML, it will save your lot of time and effort... :)
here i am uploading an XML from VF page using
<apex:inputfile>tag
and showing the result on same VF page in <apex:pageblocktable>.
and showing the result on same VF page in <apex:pageblocktable>.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="ReadXMLController"> | |
<apex:form> | |
<apex:pageblock title="Read XML" id="PB"> | |
<!-- inputFile for uploading XML --> | |
<apex:pageblocksection > | |
<apex:pageblocksectionitem> | |
<apex:outputLabel value="Please Select XML File:"/> | |
<apex:inputFile value="{!XMLBody}"> </apex:inputFile> | |
</apex:pageblocksectionitem> | |
</apex:pageblocksection> | |
<!-- Table to show the XML Result --> | |
<apex:pageblocksection title="Result of XML" columns="1" rendered="{!contactList.size != null}"> | |
<apex:pageblocktable value="{!contactList}" var="con"> | |
<apex:column value="{!con.fName}" headerValue="First Name"/> | |
<apex:column value="{!con.LName}" headerValue="Last Name"/> | |
<apex:column value="{!con.Position}" headerValue="Position"/> | |
<apex:column value="{!con.Manager}" headerValue="Manager"/> | |
</apex:pageblocktable> | |
</apex:pageblocksection> | |
<!-- Button for calling method of controller --> | |
<center> | |
<apex:commandButton value="Read" action="{!readXML}"/> | |
</center> | |
</apex:pageblock> | |
</apex:form> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ReadXMLController{ | |
public Blob XMLBody{get;set;} | |
public List<contactName> contactList{get;set;} | |
public class contactName{ | |
public String fName{get;set;} | |
public String LName{get;set;} | |
public String Manager{get;set;} | |
public String Position{get;set;} | |
} | |
public void readXML(){ | |
// you can also use here your rest response like res.getBody() | |
parseXMLResponse(XMLBody.toString()); | |
} | |
void parseXMLResponse(String XMLResp){ | |
XmlStreamReader reader = new XmlStreamReader(XMLResp); | |
contactList = new List<contactName>(); | |
integer i = 0; | |
while(reader.hasNext()){ | |
if (reader.getEventType() == XmlTag.START_ELEMENT && reader.getLocalName() == 'row') { | |
contactName con = new contactName(); | |
while(reader.hasNext()){ | |
if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getAttributeValueAt(0) == 'First Name'){ | |
reader.next(); | |
con.FName = reader.getText(); | |
} | |
if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getAttributeValueAt(0) == 'Last Name'){ | |
reader.next(); | |
con.LName = reader.getText(); | |
} | |
if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getAttributeValueAt(0) == 'Reporting Manager'){ | |
reader.next(); | |
con.Manager = reader.getText(); | |
} | |
if(reader.getEventType() == XmlTag.START_ELEMENT && reader.getAttributeValueAt(0) == 'Position'){ | |
reader.next(); | |
con.Position = reader.getText(); | |
} | |
if(reader.getEventType() == XmlTag.END_ELEMENT && reader.getLocalName() == 'row'){ | |
contactList.add(con); | |
break; | |
} | |
reader.next(); | |
} | |
} | |
reader.next(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<response uri="/crm/private/xml/Contacts/getSearchRecords"> | |
<result> | |
<Contacts> | |
<row no="1"> | |
<FL val="First Name"><![CDATA[anurag]]></FL> | |
<FL val="Last Name"><![CDATA[jain]]></FL> | |
<FL val="Position"><![CDATA[CEO]]></FL> | |
<FL val="Reporting Manager"><![CDATA[KUNAL BAJAJ]]></FL> | |
</row> | |
<row no="2"> | |
<FL val="First Name"><![CDATA[KUNAL]]></FL> | |
<FL val="Last Name"><![CDATA[BAJAJ]]></FL> | |
<FL val="Position"><![CDATA[HEAD OF VAS]]></FL> | |
<FL val="Reporting Manager"><![CDATA[lokesh mughal]]></FL> | |
</row> | |
<row no="3"> | |
<FL val="First Name"><![CDATA[sachin]]></FL> | |
<FL val="Last Name"><![CDATA[asda]]></FL> | |
<FL val="Position"><![CDATA[CMO]]></FL> | |
<FL val="Reporting Manager"><![CDATA[KUNAL BAJAJ]]></FL> | |
</row> | |
<row no="4"> | |
<FL val="First Name"><![CDATA[lokesh]]></FL> | |
<FL val="Last Name"><![CDATA[mughal]]></FL> | |
<FL val="Position"><![CDATA[CEO]]></FL> | |
<FL val="Reporting Manager"><![CDATA[KUNAL BAJAJ]]></FL> | |
</row> | |
<row no="5"> | |
<FL val="First Name"><![CDATA[ravi]]></FL> | |
<FL val="Last Name"><![CDATA[kant]]></FL> | |
<FL val="Position"><![CDATA[Specialist]]></FL> | |
<FL val="Reporting Manager"><![CDATA[anurag jain]]></FL> | |
</row> | |
<row no="6"> | |
<FL val="First Name"><![CDATA[lashman]]></FL> | |
<FL val="Last Name"><![CDATA[ghurum]]></FL> | |
<FL val="Position"><![CDATA[CFO]]></FL> | |
</row> | |
<row no="7"> | |
<FL val="First Name"><![CDATA[anuj]]></FL> | |
<FL val="Last Name"><![CDATA[kumar]]></FL> | |
<FL val="Position"><![CDATA[Manager]]></FL> | |
<FL val="Reporting Manager"><![CDATA[ravi kant]]></FL> | |
</row> | |
</Contacts> | |
</result> | |
</response> |
Screen Shots:-
Happy Coding...:)
Thanks
Anurag Jain