Written by

MOD
Question water huang · Dec 5, 2023

use %XML.Reader to parse XML to object

hi there,when object export to xml,i know that,it will call propertynameLogicalToXSD method,but i can`t find out that use %XML.Reader to parse XML to object  witch method will be called before ,

simple code like this:

Class Samples.NewClass2 Extends (%Persistent,, %XML.Adaptor)
{
Property OPDT As %Library.DateTime;
}

my xml is 

<NewClass2><OPDT>2023-11-30 11:07:02</OPDT></NewClass2>

and how parse it to object ,and not modify the xml?

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

Enrico Parisi · Dec 6, 2023

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

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

ClassMethod LogicalToXSD(%valAs%TimeStamp) As%String [ ServerOnly = 1 ]
{
    Set%val=##class(%Library.TimeStamp).LogicalToXSD(%val)
    Quit$translate(%val,"TZ"," ")
}

ClassMethod XSDToLogical(%valAs%String) As%TimeStamp [ ServerOnly = 1 ]
{
    Set$e(%val,11)="T"Quit##class(%Library.TimeStamp).XSDToLogical(%val)
}

}

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. From the documetation:

%DateTime is the same as %TimeStamp (is a sub-class or %TimeStamp) with extra logic in the DisplayToLogical and OdbcToLogical methods to handle imprecise datetime input T-SQL applications are accustomed to.

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

After that:

USER>Set reader = ##class(%XML.Reader).%New() 
USER>Set sc=reader.OpenString("<NewClass2><OPDT>2023-11-30 11:07:02</OPDT></NewClass2>") 
USER>Do reader.CorrelateRoot("Samples.NewClass2") 
USER>Do reader.Next(.ReturnObject,.sc) 
USER>Do ReturnObject.XMLExport(,",indent")
<NewClass2>
  <OPDT>2023-11-30 11:07:02</OPDT>
</NewClass2>
 
USER>Write ReturnObject.OPDT
2023-11-30 11:07:02
USER>

Enrico

0
water huang  Dec 6, 2023 to Enrico Parisi

Enrico,thanks a lot!i got it!

0