use %XML.Reader to parse XML to object
hi there,when object export to xml,i know that,it will call
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
I would create my "custom" datatype extending %Library.DateTime:
Class Community.dt.CustomDateTime Extends %Library.DateTime { ClassMethod LogicalToXSD(%val As %TimeStamp) As %String [ ServerOnly = 1 ] { Set %val=##class(%Library.TimeStamp).LogicalToXSD(%val) Quit $translate(%val,"TZ"," ") } ClassMethod XSDToLogical(%val As %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
Enrico,thanks a lot!i got it!
Hi Water,
You can use the Method Correlate form %XML.Reader class.
See the docuemtnation about importing XML into Objects.
thank you,Enrico Parisi gives me a better answer!