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
code developercode developer 

Xml to Apex class deserialise

Hi,

 

We have https://json2apex.herokuapp.com  to deserialize json to apex class.

 

Similarly I need to deserialise XML to apex class.

 

Are there any tools like above mentioned url to deserialise xml to apex.

 

Thanks

Vinita_SFDCVinita_SFDC

Hello,

 

I could not find any tool for desealizing XML to Apex class. I would suggest you to simply make use of XMLStreamReader class to read the XML and then you can use the result, also if you want to convert XML simply to string then you can use toXmlString from Documnet class.

 

Refer:http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_XmlStream_reader.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_dom_document.htm

 

 

Shaun RipleyShaun Ripley
For anyone else coming across this forum and looking for a solution. There is a library available here:
https://github.com/zabroseric/sfdc-xml-parser

It offers both serialization and deserialization. An example of your use case can be seen below:
Contact contact = (Contact) XML.deserialize('<Contact><attributes><type>Contact</type><url>/services/data/v48.0/sobjects/Contact/0032w000005DrR2AAK</url></attributes><FirstName>First</FirstName><LastName>Last</LastName><Id>0032w000005DrR2AAK</Id></Contact>')
    .setType(Contact.class).toObject();