Update existing objects from XML

Primary tabs


The example in the documentation to %XML.Reader, does not work for you?

    #include %occStatus
    // Create a new XML Reader class
    Set reader = ##class(%XML.Reader).%New()
    // Begin processing of the XML input
    Set sc=reader.OpenFile(filename)
    If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit  
    // Associate a class name with the XML element name
    Do reader.Correlate("Person","Sample.Person")
    // read Sample.Person objects from xml file
    Set Count=0
    While reader.Next(.person,.sc) {
        Write person.Name_" imported.",!
        Set Count=Count+1
        Set sc=person.%Save()
        If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit  
    If $$$ISERR(sc) Do $system.OBJ.DisplayError(sc) Quit  
    Write Count_" Sample.Person instances found."

It would create a new object.

I want to update already existing object.

Perhaps something along these lines...

ClassMethod UpsertXML(xml, wrapper, class, id, Output object) As %Status
  set reader=##class(%XML.Reader).%New()
  do reader.Correlate(wrapper,class)
  do reader.Next(.object,.sc)
  if id'="" do object.%IdSet(id)
  quit object.%Save()


w ##class(Foo.Person).UpsertXML("<Person><FirstName>Bob</FirstName><LastName>Smith</LastName><City>Paris</City></Person>","Person","Foo.Person",1)