Posts:
Replies:
Thembelani Mlalazi · Jul 16, 2020
Thembelani Mlalazi · Jul 15, 2020
@Eduard Lebedyuk
I have classes with the similar structure in your test.Serial class I have Two properties Serial1 and Serial2 as test.Serial1 and 2 respectively and i would like to print out XML maybe populate Serial 1 and have Serial2 as an empty object and Leave it out when printing.How do I controll that ?,as it stands it prints out the empty elements even when ignore null is set.Please not all is the same as above so I am printing from a persistent Level.The difference being I have two serial objects properties In my test.Serial equavalent
Thembelani Mlalazi · Dec 16, 2019
This will do exactly what you want any problems give us shout
ClassMethod ImpCSV(fileName As %String = "C:\filepath where csv file is")
{
;;Refresh globals data at the begin of proecess
;;globals hold the file being written to data and the count of lines each file has
k ^ufile,^ufile2,^counter,^counter2
set Reader =##class(%Stream.FileCharacter).%New()
;;link to the file to read
set status=Reader.LinkToFile(fileName)
if $$$ISERR(status)
{
do $System.Status.DisplayError(status)
}
;;initialise the counter for the file lines to write to one file
set ^counter =0
set ^counter2 =0
set ^ufile=""
set ^ufile2=""
;;the count for the lines read in
set lineCount=0
;;start process the lines
while 'Reader.AtEnd
{
;;read the incoming file
set line=Reader.ReadLine()
;;increment the line count by one as you read the next line
set lineCount=lineCount+1
;;piece the column to check
;;one is for the first column and so on
;;string at after the equal sign is the value to check for
if ($piece(line,",",1)="Religion")
{
;;increment count for this file
set ^counter=^counter+1
;;the counter is at one to assign the file path to write to
if (^counter=1)
{
;;set the file name to write to the global
set ^ufile=..writeFileOut("fileone",".csv",line,,^counter)
}
else
{
;;check the count has no reached the limit for the number of lines to write to
if (^counter=51)
{
;;reset the count if limit reach
set ^counter=1
;;new file
set ^ufile =..writeFileOut("fileone",".csv",line,,^counter)
}
else
{
;;use the current file
do ..writeFileOut(,,line,,^counter,"REG")
}
}
}
;;second check for the next set of data if you have more than
;;two to check for copy the below and change accordingly to accommodate the next check
;;same as above
if ($piece(line,",",1)="description")
{
;;set attributes for the second file to write to
set ^counter2=^counter2+1
if (^counter2=1)
{
set ^ufile2=..writeFileOut("fileTwo",".csv",line,,^counter2)
}else
{
if (^counter2=51)
{
set ^counter2=1
;;new file
set ^ufile2= ..writeFileOut("fileTwo",".csv",line,,^counter2)
}else
{
do ..writeFileOut(,,line,,^counter2,"DESC")
}
}
}
}
kill ^ufile,^ufile2
}
ClassMethod writeFileOut(filename, fileext As %String, line As %String, directory = "C:\directory to store files", linecount As %Integer, FileToUse)
{
set oLF = ##class(%Library.File).%New()
if (linecount=1)
{
Set filenam=directory_filename_$i(^timmmy)_fileext
set oLF.Name=filenam
if (oLF.Open("wns"))
{
set linecount=linecount+1
do oLF.WriteLine(line)
do oLF.Close()
;;w oLF.Name
quit oLF.Name
}
}else
{
if (FileToUse="DESC")
{
if (^ufile2'="")
{
set oLF.Name=^ufile2
}
}elseif(FileToUse="REG")
{
if (^ufile'="")
{
set oLF.Name=^ufile
}
}
If (##class(%File).Exists(oLF.Name))
{
Do oLF.Open("EWA")
if (linecount'=51)
{
set linecount=linecount+1
do oLF.WriteLine(line)
do oLF.Close()
quit oLF.Name
}
}
}
}
Followers:
Following:
Thembelani has not followed anybody yet.
Global Masters badges:







@Eduard Lebedyuk @Cristiano Silva
Sorry if I was not clear I still need the whole XML Serial B and SerialC if populated and if I populate One segment I do not need the Other to appear.