Hello community,
yesterday I wrote about the possibility to convert a CSV file here. I wrote here about the possibility how to upload CSV files via ABAP direct into SAP tables. Also I wrote here about the possibility to use local databindings without SAP Gateway and OData in UI5 context and here how to get the data via a modified RFC_READ_TABLE. Now I want to close this chain on another way. Here now two scripts to change the format of a CSV file, on the one hand to JSON, on the other hand to XML. With this scripts it is not necessary to implement any ABAP functions. You can download a table via SE16, delete the first, third and last row, replace | with ; and convert the CSV file with the scripts from here. Now you can change the format to JSON or XML easily.
Change format from CSV to JSON
//-Begin---------------------------------------------------------------- //-Directives--------------------------------------------------------- #AppType Console #Option Explicit //-Sub ToJSON--------------------------------------------------------- Sub ToJSON(FileName As String) //-Variables------------------------------------------------------ Dim hFileIn As Integer Dim hFileOut As Integer Dim HeadLine As String Dim Row As String Dim Col Dim cntLines As Integer Dim i As Integer Dim j As Integer hFileIn = FileOpen(FileName, Input) hFileOut = FileOpen(FileName & ".json", Output) If hFileIn And hFileOut Then Do Until FileEof(hFileIn) Row = FileInput(hFileIn) If Row <> "" Then cntLines = cntLines + 1 End If Loop cntLines = cntLines - 1 // Headline FileSeek(hFileIn, 0) FilePrint(hFileOut, "{") FilePrint(hFileOut, " " & DQ & UCase(FileName) & DQ & " : [") HeadLine = FileInput(hFileIn) Dim Head[] = Split(HeadLine, ";") j = 1 Do Until FileEof(hFileIn) Row = FileInput(hFileIn) If Row <> "" Then Dim Cols[] = Split(Row, ";") FilePrint(hFileOut, " {") i = 0 ForEach Col In Cols Row = Row & Col & ";" If i < UBound(Head) Then FilePrint(hFileOut, " " & DQ & Head[i] & DQ & _ " : " & Col & ",") Else FilePrint(hFileOut, " " & DQ & Head[i] & DQ & _ " : " & Col) End If i = i + 1 Next If j < cntLines Then FilePrint(hFileOut, " },") Else FilePrint(hFileOut, " }") End If j = j + 1 End If Loop FilePrint(hFileOut, " ]") FilePrint(hFileOut, "}") FileClose(hFileIn) FileClose(hFileOut) End If End Sub //-Sub Main----------------------------------------------------------- Sub Main() ToJSON("sflight.csv") Pause End Sub
//-End------------------------------------------------------------------
Change format from CSV to XML
//-Begin---------------------------------------------------------------- //-Directives--------------------------------------------------------- #AppType Console #Option Explicit //-Sub ToXML---------------------------------------------------------- Sub ToXML(FileName As String) //-Variables------------------------------------------------------ Dim hFileIn As Integer Dim hFileOut As Integer Dim HeadLine As String Dim Row As String Dim Col Dim i As Integer hFileIn = FileOpen(FileName, Input) hFileOut = FileOpen(FileName & ".xml", Output) If hFileIn And hFileOut Then FilePrint(hFileOut, "<?xml version=" & DQ & "1.0" & DQ & _ " encoding=" & DQ & "ISO-8859-1" & DQ & "?>") FilePrint(hFileOut, "<root>") HeadLine = FileInput(hFileIn) Dim Head[] = Split(HeadLine, ";") Do Until FileEof(hFileIn) Row = FileInput(hFileIn) If Row <> "" Then Dim Cols[] = Split(Row, ";") FilePrint(hFileOut, " <item>") i = 0 ForEach Col In Cols Row = Row & Col & ";" FilePrint(hFileOut, " <" & Head[i] & ">" & _ Col & "</" & Head[i] & ">") i = i + 1 Next FilePrint(hFileOut, " </item>") End If Loop FilePrint(hFileOut, "</root>") FileClose(hFileIn) FileClose(hFileOut) End If End Sub //-Sub Main----------------------------------------------------------- Sub Main() ToXML("sflight.csv") Pause End Sub
//-End------------------------------------------------------------------
With the combination of standard SAP functionalities, ABAP and a scripting language you can perfom powerful and easy solutions fast.
Enjoy it.
Cheers
Stefan





