Written by

Question water huang · Nov 30, 2023

property of %Library.DateTime export to xml

I create a class, property OPDT as %Library.DateTime,the class extends %XML.Adaptor,after run " d obj.XMLExportToString(.xml)" OPDT`s value is 2023-11-28T13:57:26,but what i need is 2023-11-28 13:57:26,so is there any solution?

Product version: Ensemble 2016.1
$ZV: Cache for Windows (x86-64) 2016.2.3 (Build 907_11_20446U) Thu Nov 12 2020 16:56:45 EST

Comments

Ashok Kumar T · Nov 30, 2023

Hello @water huang,

The default implementation of the XMLExportToString in %XML.Adaptor  basically converts the date format as 2023-11-28T13:57:26Z for the property value by the logic $translate(value," ","T")_"Z"). So, I guess you may need to convert by own.

0
Enrico Parisi · Nov 30, 2023

I would create my "custom" datatype extending %Library.DateTime:

Class Community.dt.CustomDateTime Extends%Library.DateTime
{

/// Converts the %TimeStamp value to the canonical SOAP encoded value.ClassMethod LogicalToXSD(%valAs%TimeStamp) As%String [ ServerOnly = 1 ]
{
	Set%val=##class(%Library.TimeStamp).LogicalToXSD(%val)
	Quit$translate(%val,"TZ"," ")
}

}

Then in your class define your property as:

Property OPDT As Community.dt.CustomDateTime;

Are you sure you really need %Library.DateTime and not %Library.TimeStamp?
The difference is the JDBC/ODBC format.

If you prefer using %Library.TimeStamp, then change the superclass in my sample code.

Enrico

0
Ashok Kumar T  Nov 30, 2023 to Enrico Parisi

Hello @Enrico Parisi 

That's right, However Instead of creating a custom datatype to fix this. create/use the auto generated method for that property "OPDTLogicalToXSD" and implement the code logic.

ClassMethod OPDTLogicalToXSD(%valAs%TimeStamp) As%String [ ServerOnly = 1 ]
{
	Set%val=##class(%Library.TimeStamp).LogicalToXSD(%val)
	Quit$translate(%val,"TZ"," ")
}
0
water huang  Dec 3, 2023 to Enrico Parisi

Enrico,thank you in fact ,i need TimeStamp!

0
Ashok Kumar T · Nov 30, 2023

Hello @water huang 

As You can create a class method with name of your property "OPDTLogicalToXSD" and add the code conversion for the datetime as mentioned by @Enrico Parisi at XML export time.

It's suitable for both XML and JSON adaptor.

Sample code.

Class Samples.NewClass2 Extends (%Persistent, %Populate, %JSON.Adaptor, %XML.Adaptor)
{
Property OPDT As%Library.DateTime;ClassMethod OPDTLogicalToXSD(%valAs%TimeStamp) As%String [ ServerOnly = 1 ]
{
	Set%val=##class(%Library.TimeStamp).LogicalToXSD(%val)
	Quit$translate(%val,"TZ"," ")
}
}

output

<NewClass2><OPDT>2023-11-30 11:07:02</OPDT></NewClass2>
0
Enrico Parisi  Nov 30, 2023 to Ashok Kumar T

They are both possible solutions, however if you create a custom datatype you can then use it in any other property and class, without the need to implement <PropertyName>LogicalToXSD() for each property in each class.

Enrico

0
water huang  Dec 3, 2023 to Ashok Kumar T

Ashok,thank you,this is what i asked,in fact ,i should change  %Library.DateTime to and %Library.TimeStamp 
 

0
water huang  Dec 5, 2023 to Ashok Kumar T

and how to parse "<NewClass2><OPDT>2023-11-30 11:07:02</OPDT></NewClass2>" to a object as type of Samples.NewClass2?

0