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
melmoussaouimelmoussaoui 

Creating an XML with Xmlstreamwriter

Hello, I'm new to apex and salesforce world and I'm trying to create an XML file using Xmlstreamwriter which should look like this :

 

<?XML version="1.0" encoding="utf-8"?>
<SERVICE version="1.0">
	<CLIENT email="xx.xx@xx.xx">
		<BASIC_FIELDS id="1">
			<ADDRESS_LINE_1 id="2" >24 rue de rocher</ADDRESS_LINE_1>
			<CITY id="3" >Paris</CITY>
                 </BASIC_FIELDS>
        </CLIENT>
</SERVICE>

 

 

but when I try this code :

 

 public String getxmlRes()
        {
        Xmlstreamwriter xmlW = new Xmlstreamwriter();
        xmlW.writeStartDocument('utf-8','1.0');
        xmlW.writeStartElement('SERVICE', 'version', '"1.0"');    
        xmlW.writeStartElement(null,'CLIENT email= xxx@xx.com',null);
        xmlW.writeStartElement(null,'BASIC_FIELDS id="1"',null);
        xmlW.writeAttribute(null, null, 'ADRESS_LINE id="2"', '24 rue de rocher');
        xmlW.writeAttribute(null, null,'CITY id="3"', 'Paris');
        xmlW.writeEndElement();
        xmlW.writeEndElement(); 
        xmlW.writeEndElement();
        xmlW.writeEndDocument();
        String xmlStringxmlRes = xmlW.getXmlString();
        System.debug('The XML :'+xmlW.getXmlString());     
        xmlW.close();
        return xmlStringxmlRes;
}

 

instead I get this :

 

<?xml version="1.0" encoding="utf-8"?>
<SERVICE:version>
<CLIENT email= xxx@xx.com>
<BASIC_FIELDS id="1" ADRESS_LINE id="2"="24 rue de rocher" CITY id="3"="Paris">
</BASIC_FIELDS id="1">
</CLIENT email= xxx@xx.com>
</SERVICE:version>

i hope someone could help me!

thanks

 

CaptainObviousCaptainObvious

Give this a try:

 

public String getxmlRes() {

	Xmlstreamwriter xmlW = new Xmlstreamwriter();
	xmlW.writeStartDocument('utf-8','1.0');
		xmlW.writeStartElement(null,'SERVICE', null);
			xmlW.writeAttribute(null,null,'version','1.0');
			xmlW.writeStartElement(null,'CLIENT',null);
				xmlW.writeAttribute(null,null,'email','xx.xx@xx.xx');
				xmlW.writeStartElement(null,'BASIC_FIELDS',null);
					xmlW.writeAttribute(null,null,'id','1');

					xmlW.writeStartElement(null,'ADRESS_LINE_1',null);
					xmlW.writeAttribute(null,null,'id','2');
						xmlW.writeCharacters('4 rue de rocher');
					xmlW.writeEndElement(); //Close ADRESS_LINE_1

					xmlW.writeStartElement(null,'CITY',null);
						xmlW.writeAttribute(null,null,'id','3');
						xmlW.writeCharacters('Paris');
					xmlW.writeEndElement(); //Close CITY

				xmlW.writeEndElement(); //Close BASIC_FIELDS
			xmlW.writeEndElement(); //Close CLIENT
		xmlW.writeEndElement(); //Close SERVICE
	xmlW.writeEndDocument(); //Close XML document

	String xmlStringxmlRes = xmlW.getXmlString();
	System.debug('The XML :'+xmlW.getXmlString());     
	xmlW.close();

	return xmlStringxmlRes;
}

 

 

melmoussaouimelmoussaoui

Thank you, that what i did and it works !

Pavo83Pavo83

Hi,

 

Excuse if I re-up this old topic, but I have to ask:

How do you apply this apex code into a button to create the xml file?

San36San36

Thanks for your example. It worked perfect. 

Do you have any idea on how to post generated xml file to Internal network(specified location)? 

I believe we can implement this using Http post method...but what would be the format for setendpointurl?

 

 

 

Thanks in Advance.............

arunadeveloperarunadeveloper
Hi , did you get a answer for this question. how did you generated xml file up on button click.
can you please send me code or give me an idea how to do that.
kirank2kirank2
Hi

Does anyone aware of any tool which will provide the code from xml to Xmlstreamwriter .. thanks