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?
Comments
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
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!