Question
· Oct 13, 2017

CSVFile straight TO a XML File

I am trying to read a csv file and transfer it to a XML file without storing the objects to a database I have this code here doing the reading and have another method transferring the object read to a file but the reading one reads fine when it comes to the converting one I run to problems nothing happens any help appreciated

ClassMethod Write(obj, directory As %String = "C:\40_Test\") As %Status
{
    set writer=##class(%XML.Writer).%New()
    set writer.Indent=1

  
    // set the file
 
     Set filename=directory_"Generatedfile"_".xml"
    set status=writer.OutputFilename(filename)
    if $$$ISERR(status) {
        do $System.Status.DisplayError(status)
        quit $$$ERROR($$$GeneralError, "Output destination not valid")
    }

    set status=writer.RootObject(obj)
    if $$$ISERR(status) {
        do $System.Status.DisplayError(status)
        quit $$$ERROR($$$GeneralError, "Error writing root object")
    }
       
    quit status
}

//reads in a csv file
ClassMethod ImportCSV(pInput As %Stream.Object)
{
   set adapter =##class(%Stream.FileCharacter).%New()
  
  set status=adapter.LinkToFile(pInput)
  
  if $$$ISERR(status)
  {
   do $System.Status.DisplayError(status)
  }
  
  set counter =1
  
  while 'adapter.AtEnd
  {
   set line=adapter.ReadLine()
   
   set rec= ##class(TestEnvironment.HRecord).%New()
   
   set rec.OrCode=$piece(line,",",1)
   set rec.Name=$piece(line,",",2)
   set rec.Type=$piece(line,",",3)
   set rec.Security=$piece(line,",",4)
   set rec.Age=$piece(line,",",5)
   set rec.Gender=$piece(line,",",6)
   set rec.TotalCount=$piece(line,",",7)
   set rec.BOccupied=$piece(line,",",8)
   set rec.DTBCount=$piece(line,",",9)
   set rec.LBCount=$piece(line,",",10)
   set rec.Available=$piece(line,",",11)
   set rec.Warn=$piece(line,",",12)
   set rec.LastU=$piece(line,",",13)
   

         Set savestatus=rec.%Save()
              
      if $$$ISERR(savestatus)
    {
     do $System.Status.DisplayError(status)
    }
   
    }
   
    //if error found during processing,show it
    if $$$ISERR(status)
    {
     do $System.Status.DisplayError(status)
    }
   
    //Check for error during convertion
     set convert=1
    Try
    {
     set convert = ##class(TestEnvironment.HRecord).Write(rec)
     }
     Catch
     {
      set convert=0
     }
    
    If (convert '=1)
    {
        Write "Convertion did not take place "
        Quit
        }
}
Discussion (2)2
Log in or sign up to continue

Hi Thembelani!

Beautified your code a bit with code blocks.

To post code blocks highlighted do the following:

1. Paste the code

2. Select code and pick Formatted from Paragraph format. (Normal dropdown).

3. Pick Computed code from Block Styles (Styles dropdown).

Or, in the source mode (click Disable rich-text or click Source)  embrace code block with <pre><code> </code><pre> tags.

See details in this post.