You need to sign in to do that
Don't have an account?

How to generate XML file using Apex class
Hi There,
I have to generate a XML file.
when I click on button button should generate a xml schema file.
I am able to create a xml file with the help of "XmlStreamWriter" class.
and able to print in system.debug. but i want to generate xml schema or what ever I am printing in system debug generate as xml file when i click on button.
Example below xml generate for teh conroller
XmlStreamWriter w =newXmlStreamWriter();
w.writeStartDocument(null,'1.0 encoding=UTF-8');
w.writeStartDocument(null,'<soap:Envelope xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">');
w.writeStartElement(null,'Enrol_Event__c ',null);
for(Enrol_Event__cenrollEvent : getEnrollmentEventList()){
w.writeStartElement('my','LANID','my');
w.writeCharacters('12345678910');
w.writeEndElement();
w.writeStartElement('my','LegalCompanyName','my');
w.writeCharacters(enrollEvent.PREF_CO_NM__c);
w.writeEndElement();
w.writeStartElement('my','EnrollmentManagerName','my');
w.writeCharacters(enrollEvent.DSPCH_REP_ID__r.Name);
w.writeEndElement();
w.writeStartElement('my','ExceptionsSection','my');
w.writeStartElement('my','ExceptionsDetails','my');
w.writeCharacters(enrollEvent.EXCPT_DET_TXT__c);
w.writeEndElement();
w.writeStartElement('my','ExceptionsApprovedBy','my');
w.writeCharacters(enrollEvent.EXCPT_APRVD_NM__c);
w.writeEndElement();
w.writeEndElement();
w.writeEndElement();
w.writeEndDocument();
string xml = w.getXmlString();
w.close();
system.debug(xml);
out put
<?xml version="1.0" encoding="UTF-8"?>
<my:myField>
<my:LANID>12345678910</my:LANID>
<my:LegalCompanyName>Preferred Company Name</my:LegalCompanyName>
<my:EnrollmentManagerName>Betsy Barrett</my:EnrollmentManagerName>
<my:ExceptionsSection>
<my:ExceptionsDetails>The cost of the BC is waived for this event.</my:ExceptionsDetails>
<my:ExceptionsApprovedBy>Katie Anderson</my:ExceptionsApprovedBy>
</my:ExceptionsSection>
</my:myField>
can any one suggest me how to generate xml schema file or xml file when i click on button.
You may want to look at the Document object and use it to create a document out of your XML string. All of this could be upon an action method tied to a button click.
best,
Ram
Something like this:
Document document = new Document();
document.AuthorId = UserInfo.getUserId();
document.FolderId = UserInfo.getUserId();
String bodyStr = 'Hello World<Test>Value</Test>';
Blob body = Blob.valueOf(bodyStr);
document.Body = bodyStr;
insert document;
Ram
Hi Ram,
Thank you for your answer , I am able to insert but I am not getting the XML formate which I am expecting .
it is inseted the document as a single line for example
<?xml version="1.0" encoding="UTF-8"?><my:myField><my:LANID>12345678910</my:LANID><my:LegalCompanyName>Preferred Company Name</my:LegalCompanyName><my:EnrollmentManagerName>qwewqeqwee</my:EnrollmentManagerName</my:myField>
but I want xml formate
<?xml version="1.0" encoding="UTF-8">
<my:myField>
<my:LANID>12345678910</my:LANID>
<my:LegalCompanyName>Preferred Company Name</my:LegalCompanyName>
<my:EnrollmentManagerName>qwewqeqwee</my:EnrollmentManagerName>
</my:myField>
so can you please let me know how can i get the xml document formate.
Aruna,
Just add a new line character (\n) to wherever you need a line break.
so something like this:
String bodyStr = 'Hello World\n<Test>Value</Test>\n<Test>Value2</Test>\n<Test>Value3</Test>';
This gives you a file like this:
Hi there,
Thanks "Ram" it's worked . :)
Excellent - glad to hear it, Aruna :)
Can you please send me the sample code to generate XML file or schema from Batch Class ?
mohdtabrezalam511@gmail.com
Could you please let me know if you are doing any integration in the code. I mean the xml which is generated, are you sending it to any external system ?
Kindly share your code as well if possible to prsingh218@gmail.com.
I'll be waiting for your response. Thanks