miércoles, 10 de noviembre de 2010

XML and RegEx again...


Today I was thinking about XML and RegEx...You can parse an XML document using RegEx, not the best way, but surely is fun...so...what about creating an XML file using RegEx? More fun for sure! -:D Of course...and just to make it clear...there are other more standard and easier ways to achieve the same: Simple Transformations, XLST Transformation y XML DOM.

But as always...this code was very fun to made -;)


TYPES: BEGIN OF TY_XML,
FIELD TYPE STRING,
END OF TY_XML.

DATA: T_DD03P_TAB TYPE STANDARD TABLE OF DD03P,
T_FILETAB TYPE FILETABLE,
T_XML TYPE STANDARD TABLE OF TY_XML.

DATA: L_FLAG TYPE C,
L_AUX TYPE STRING,
L_STRING TYPE STRING,
L_XML_LINE TYPE STRING,
DATA_TAB TYPE REF TO DATA,
SIZE TYPE I,
W_SUBRC TYPE SY-SUBRC,
W_FILE_OUT TYPE STRING.

FIELD-SYMBOLS: TYPE STANDARD TABLE,
TYPE ANY,
TYPE ANY,
LIKE LINE OF T_XML,
LIKE LINE OF T_DD03P_TAB,
LIKE LINE OF T_FILETAB.

SELECTION-SCREEN BEGIN OF BLOCK TEST WITH FRAME.
PARAMETERS:
TABNAME TYPE DD02L-TABNAME,
FILE_OUT LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK TEST.

START-OF-SELECTION.
PERFORM VALIDATE_TABLE USING TABNAME
CHANGING L_FLAG.
IF L_FLAG EQ SPACE.
PERFORM LOAD_TABLE_STRUCT USING TABNAME.
PERFORM LOAD_TABLE_INFO USING TABNAME.
PERFORM CREATE_XML.
PERFORM DOWNLOAD_XML.
ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FILE_OUT.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
EXPORTING
WINDOW_TITLE = 'Seleccionar archivo'
DEFAULT_FILENAME = '*.xml'
FILE_FILTER = '*.xml'
CHANGING
FILE_TABLE = T_FILETAB
RC = W_SUBRC.

READ TABLE T_FILETAB INDEX 1
ASSIGNING .
FILE_OUT = .
W_FILE_OUT = FILE_OUT.

IF FILE_OUT IS INITIAL.
EXIT.
ENDIF.

*&------------------------------------------------------*
*& Form VALIDATE_TABLE *
*&------------------------------------------------------*
FORM VALIDATE_TABLE USING P_TABNAME
CHANGING P_FLAG.

SELECT SINGLE TABNAME
INTO P_TABNAME
FROM DD02L
WHERE TABNAME EQ P_TABNAME.
IF SY-SUBRC NE 0.
P_FLAG = 'X'.
ELSE.
CLEAR P_FLAG.
ENDIF.

ENDFORM. " VALIDATE_TABLE

*&------------------------------------------------------*
*& Form LOAD_TABLE_STRUCT *
*&------------------------------------------------------*
FORM LOAD_TABLE_STRUCT USING P_TABNAME.

CALL FUNCTION 'DDIF_TABL_GET'
EXPORTING
NAME = P_TABNAME
TABLES
DD03P_TAB = T_DD03P_TAB
EXCEPTIONS
ILLEGAL_INPUT = 1
OTHERS = 2.

ENDFORM. " LOAD_TABLE_STRUCT

*&------------------------------------------------------*
*& Form LOAD_TABLE_INFO *
*&------------------------------------------------------*
FORM LOAD_TABLE_INFO USING P_TABNAME.

CREATE DATA DATA_TAB TYPE STANDARD TABLE OF (P_TABNAME).
ASSIGN DATA_TAB->* TO .
SELECT *
FROM (P_TABNAME)
INTO TABLE .

ENDFORM. " LOAD_TABLE_INFO

*&------------------------------------------------------*
*& Form CREATE_XML *
*&------------------------------------------------------*
FORM CREATE_XML.

APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = ''.
APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = ''.
*XML Logic here!
LOOP AT ASSIGNING .
L_XML_LINE = ''.
LOOP AT T_DD03P_TAB ASSIGNING .
ASSIGN COMPONENT -FIELDNAME OF
STRUCTURE TO .
L_AUX = .
CONDENSE L_AUX NO-GAPS.
CONCATENATE -FIELDNAME '/' L_AUX
INTO L_STRING.
REPLACE REGEX '(\w+)[\/](\w+)' IN L_STRING
WITH '<$1>$2'.
CONCATENATE L_XML_LINE L_STRING INTO L_XML_LINE.
ENDLOOP.
APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = L_XML_LINE.
CONCATENATE -FIELD '
'
INTO -FIELD.
ENDLOOP.
*XML Logic here!
APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = '
'.

ENDFORM. " CREATE_XML

*&------------------------------------------------------*
*& Form DOWNLOAD_XML *
*&------------------------------------------------------*
FORM DOWNLOAD_XML.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
EXPORTING
BIN_FILESIZE = SIZE
FILENAME = W_FILE_OUT
FILETYPE = 'DAT'
CHANGING
DATA_TAB = T_XML.

ENDFORM. " DOWNLOAD_XML



Greetings,

Blag.