+ Start a Discussion
KumarhattiKumarhatti 

Recieving XML in Webservice using Rest architecture??

Hi,

I wrote a webservice which uses RestRequest to recieve the xml, however I am not able to convert the recieved request into xml i.e Dom.document or xmlstreamreader, below is the webservice I wrote, I able to see the xml in the log System.debug('--->RestContext.request'+req); but How do I convert RestContext.request to dom or xmlstreamreader, as I need to parse and create record from the data??, Thanks a lot

 

@RestResource(urlMapping='/CandExpJobPost_Router/v1/*')
global with sharing class CandExpJobPost_RouterV1 {

static boolean LogEvent = false;

CandExpJobPost_RouterV1(){System.debug('----->ssip');}


@HttpPost
global static String doRecieve( ) {
mydef doc3;
createjobpostfromxml parse=new createjobpostfromxml();
//System.debug('Recieved xml -->'+doc3);
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
String xml = req.requestBody.toString().trim();
System.debug('--->RestContext.request'+req);

System.debug('--->xml'+xml);
//res.addHeader('Content-Type', 'text/xml');
testdomjobpost testdoc = new testdomjobpost();
String x=String.Valueof(req);

 

}

*rdinakaran**rdinakaran*

Hi Kumarhatti,

 

Use XMLDom to Parse the response.

 

for example:

your XML response in debuglog is below

 

<Member> 
<Name>rdinakaran</Name> 

<TS>30000<TS> 
</Member>

 

then use the following code to parse it and create a record.

 

 

Management__c Members = new Management__c(); //Management is your Custom object
		
XMLDom responseXML = new XMLDom(res);   //Your Response as Parameter
 
//Get Member Details          
for(XMLdom.Element Member :responseXML.getElementsByTagName('Member')) { 
   if(Member.getValue('Name').trim() != null && Member.getValue('Name').trim() != '') {
		Members.Name= Member.getValue('Name').trim();
   } 
	if(Member.getValue('TS').trim() != null && Member.getValue('TS').trim() != '') {
		Members.Total_Salary__c= decimal.valueof(Member.getValue('TS').trim());
   }
 }  

insert Members;

 

 Let me know if any issues, or if it works ,please mark it solved. 

 

Thanks,

rdinakaran