1

I'm currently trying to determine whether we can replace our company's convoluted XML processing solution (based on XPath) with one based on @SfdcFox's XmlToJson solution.

Unfortunately, the XML we need to parse includes Date and Time as element names, e.g.:

       <RequestedCompletionDateTime>
           <Date>2019-01-28</Date>
           <Time>11:00:09Z</Time>
       </RequestedCompletionDateTime>

Is it possible to use an XML -> Json -> Object solution to parse this document? Or will this prove a blocker issue?

If so, how can we solve this? (I'm thinking maybe we could pre-process the document to rename these element, but I'm not sure that could be done in an efficient way and I'm hoping there would be a more elegant solution.)

3
  • Is that correct that main issue is that want to use JSON.deserialize into wrapper class, and that does not allow properties named Date and Time?
    – kurunve
    Feb 13 '19 at 10:28
  • Apex doesn't allow properties named Date and Time... I would like to use Json.deserialize and unless I misunderstand the code (my experience with deserialize is limited), if I can't create a model with these property names, I shouldn't experience Sfdc's native deserialize to populate values accordingly. Feb 13 '19 at 10:51
  • 1
    You are in the right direction. If there are reserved words in your input string and that you try to deserialize those, it will result in error. You will need to replace all such reserved words before deserializing the JSON.
    – Jayant Das
    Feb 13 '19 at 16:09
0

I've come to the conclusion that my question may reasonably be closed as a duplicate.

See: How do you deserialize json properties that are reserved words in apex?

0

For anyone that comes across this problem - Brian is absolutely right. Something like the below is great if you don't mind a little regex:

xmlString.replaceAll('<(/?(Date|Time))>', '<$1_x>');

The suffix added is _x, and if you want to add more words that are reserved just add another |xxx next to the Date|Time so that each word is joined via the pipe delimiter.

Alternatively, this has been added to a large XML serialization / deserialization library that natively manages reserved words. Automatically adding the suffix of __x, or any another custom one of your preference. https://github.com/zabroseric/sfdc-xml-parser

Please see the example below:

class RequestedCompletionDateTime {
    public Date date_x;
    public Time time_x;
}

RequestedCompletionDateTime compl = (RequestedCompletionDateTime) XML.deserialize(
'<RequestedCompletionDateTime>' +
'   <Date>2019-01-28</Date>' +
'   <Time>11:00:09Z</Time>' +
'</RequestedCompletionDateTime>' 
).setType(RequestedCompletionDateTime.class).toObject();

// RequestedCompletionDateTime:[date_x=2019-01-28 00:00:00, time_x=11:00:09.000Z]
System.debug(compl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.