Generating XML file in Salesforce…………………………..
Ø
We need start class with global keyword
Ø XmlStreamWriter Class
The XmlStreamWriter class provides
methods for writing XML data.
For more information:
class:
@Restresource(urlmapping
= '/accounts/*')
Global class
GenerateaccountXML {
webservice static string generatexml(){
xmlstreamwriter x = new
xmlstreamwriter();
list<account> acc = [SELECT
AccountNumber,AccountSource,BillingCity,BillingCountry,DOB__c,Email__c,Name,
(select name from contacts) FROM
Account];
string xmlstr = '';
x.writeStartElement(null, 'Accounts',
null);
for(account a : acc){
if(a.contacts.size()>0){
x.writeStartElement(null, 'Account',
null);
x.writeCharacters(a.name);
x.writeEndElement();
x.writeStartElement(null,
'Accountbillingcity', null);
if(a.BillingCity == null){
x.writeCharacters('hyderabad');
}else{
x.writecharacters(a.BillingCity);
}
x.writeEndElement();
x.writeStartElement(null,
'contacts', null);
for(contact c : a.contacts){
x.writeStartElement(null,
'contactname', null);
x.writeCharacters(c.name);
x.writeEndElement();
}
x.writeEndElement();
}
}
x.writeEndElement();
xmlstr = x.getXmlString();
return xmlstr;
}
}
Execute following code in
console :
string s =
GenerateaccountXML.generatexml();
system.debug('account xml
file is : ' +s);