function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arunadeveloperarunadeveloper 

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.

Suresh RaghuramSuresh Raghuram
Aruna would have better luck storing the Xml into a custom object and then using java to connect to that custom object, extract it, and then create the file
ForcepowerForcepower

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

ForcepowerForcepower

 

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

arunadeveloperarunadeveloper

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.

ForcepowerForcepower

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:

 

Hello World
<Test>Value</Test>
<Test>Value2</Test>
<Test>Value3</Test>
arunadeveloperarunadeveloper

Hi there,

 

Thanks "Ram" it's worked . :)

 

ForcepowerForcepower

Excellent - glad to hear it, Aruna :)

Nagesh B 13Nagesh B 13
please provide the sample code to generate XML file or schema from Batch Class
 
Nagesh B 13Nagesh B 13
my mail Id  is nag9322@gmail.com
 
Tabrez AlamTabrez Alam
Hi Arun,

Can you please send me the sample code to generate XML file or schema from Batch Class ?
mohdtabrezalam511@gmail.com
 
Priya Singh 2Priya Singh 2
Hi Aruna,

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