这个转换类是用来在银企直连接时对银行报告的XML与ABAP结构 转换的一个类,整理了一下,里面用到一些银企直连的表,现只把接收银行的XML报文转换为ABAP结构的调整了可以通用,ABAP转XML,没整理,可以自己整理一下,转JSON的就没多少意思 用这类了,我觉得直接使用/ui2/cl_json更好。参看。
创建一个表ZTRTXML_ABAP,用于在转换时使用
对在表中对每个XML转换定义一个转类型操作,比如使用中使用的
创建XML类ZCL_TR2_XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 |
class ZCL_TR2_XML definition public final create public . public section. types: BEGIN OF type_s_key_value. TYPES:fname TYPE string. TYPES:fvalue TYPE string. TYPES:END OF type_s_key_value . types: type_t_key_value TYPE STANDARD TABLE OF type_s_key_value WITH DEFAULT KEY . types: BEGIN OF type_s_ZTRTXML_ABAP. INCLUDE TYPE ZTRTXML_ABAP. TYPES: zdele TYPE abap_bool. TYPES END OF type_s_ZTRTXML_ABAP . types: ZTRTXML_ABAP_tab TYPE STANDARD TABLE OF type_s_ZTRTXML_ABAP . methods CONVERT_XML_TO_ABAP importing !RESPONSE type STRING !BANK_IND type CHAR5 optional !BANK_ACTION type CHAR4 exporting !DATA type DATA . methods CONVERT_VALUE_TO_XML importing !BANK_IND type CHAR5 optional !BANK_ACTION type CHAR4 !BUKRS type BUKRS optional !YHZH type CHAR50 optional !DATA type DATA exporting !E_MSG type BAPI_MSG !E_TYPE type BAPI_MTYPE !E_XML type STRING . methods CONVERT_VALUE_TO_JSON importing !BANK_IND type CHAR5 !BANK_ACTION type CHAR4 !DATA type DATA exporting !E_MSG type BAPI_MSG !E_TYPE type BAPI_MTYPE !E_XML type STRING . PROTECTED SECTION. private section. data GT_ZTRTXML_ABAP type ZTRTXML_ABAP_tab . methods TRANSFORMATION importing !DOCUMENT type ref to IF_IXML_DOCUMENT !BANK_IND type CHAR5 !BANK_ACTION type CHAR4 exporting !DATA type DATA . methods TRANSFORMATION_TABLE importing !BANK_IND type CHAR5 !BANK_ACTION type CHAR4 !REPEAT_NAME type STRING exporting !LINES type I changing !NODE type ref to IF_IXML_NODE !TABLE type ANY TABLE . methods CREATE_ELEMENT importing !DOCUMENT type ref to IF_IXML_DOCUMENT value(PARENT) type STRING value(NAME) type STRING value(VALUE) type STRING optional !REPEAT type ABAP_BOOL optional !REPEAT_GID type I optional !ATTRIBUTES type TYPE_T_KEY_VALUE optional returning value(E_GID) type I . methods CREATE_ELEMENT_TABLE importing !TABLE type ANY TABLE !DOCUMENT type ref to IF_IXML_DOCUMENT !BANK_IND type CHAR5 !BANK_ACTION type CHAR4 !PARENT type ZXMLPARENT !GRANDPARENT type ZXMLPARENT . methods CREATE_ELEMENT_STRUC importing !DATA type DATA !DOCUMENT type ref to IF_IXML_DOCUMENT !BANK_IND type CHAR5 !BANK_ACTION type CHAR4 !PARENT type ZXMLPARENT !REPEAT_GID type I . methods CREATE_DEFAULT_HEADER importing !DOCUMENT type ref to IF_IXML_DOCUMENT !TRNCOD type ZTRNCOD !BUKRS type BUKRS optional !YHZH type ZYHZH optional !BANK_IND type CHAR5 default 'BOC' !BANK_ACTION type CHAR4 default '0' . ENDCLASS. CLASS ZCL_TR2_XML IMPLEMENTATION. METHOD convert_value_to_json. * DATA(lo_response) = /ui2/cl_json=>serialize( * EXPORTING * data = data ** compress = ** name = * pretty_name = 'X' ** type_descr = ** assoc_arrays = ** ts_as_iso8601 = ** expand_includes = ** assoc_arrays_opt = ** numc_as_string = ** name_mappings = ** conversion_exits = ** receiving ** r_json = * ). * * "json区分大小写,需要转换,否则对方识别不到对应字段 * SELECT * * FROM ztr2t0191 * INTO TABLE @DATA(lt_trans) * WHERE bank_ind = @bank_ind * AND bank_action = @bank_action. * * LOOP AT lt_trans INTO DATA(ls_trans). * TRANSLATE ls_trans-name_ori TO LOWER CASE. * * DATA(lv_char1) = '"' && ls_trans-name_ori && '"'. * DATA(lv_char2) = '"' && ls_trans-name_trans && '"'. ** REPLACE lv_char1 INTO lo_response WITH lv_char2. modify by crh * REPLACE ALL OCCURRENCES OF lv_char1 in lo_response with lv_char2. * CLEAR: lv_char1,lv_char2. * ENDLOOP. * * e_xml = lo_response. ENDMETHOD. METHOD convert_value_to_xml. * DATA: lr_ixml TYPE REF TO if_ixml, * lr_document TYPE REF TO if_ixml_document, * lv_xml_as_string TYPE xstring, * lv_xml_size TYPE i, * lt_xml_as_table TYPE dcxmllines, * lv_cstring TYPE string. * * FIELD-SYMBOLS:<fs_value> TYPE any. * * FIELD-SYMBOLS:<ft_table> TYPE ANY TABLE, * <fs_table> TYPE any. * * DATA:lv_parent TYPE string, * lv_name TYPE string, * lv_value TYPE string. * * DATA:lt_attributes TYPE type_t_key_value, * ls_attribute TYPE type_s_key_value. * * DATA:lt_ztr2t0190 TYPE ztr2t0190_tab. * * DATA:ls_test TYPE demo_gtt. * * DATA:repeat_gid TYPE i, * repeat_name TYPE string. * * SELECT * INTO TABLE @gt_ztr2t0190 FROM ztr2t0190 * WHERE bank_ind = @bank_ind * AND bank_action = @bank_action. * SORT gt_ztr2t0190 BY bank_ind bank_action zeile. * * lr_ixml = cl_ixml=>create( ). * lr_document = lr_ixml->create_document( ). * IF bank_ind = 'BOC'. * ELSEIF bank_ind = 'CCB'. * lr_document->set_standalone( 'X' ). * * DATA(l_encoding) = lr_ixml->create_encoding( character_set = 'GB2312' * byte_order = if_ixml_encoding=>co_none ). * lr_document->set_encoding( l_encoding ). * ENDIF. * * IF gt_ztr2t0190 IS NOT INITIAL. * me->create_default_header( document = lr_document trncod = gt_ztr2t0190[ 1 ]-trncod bank_ind = bank_ind bukrs = bukrs yhzh = yhzh ). * ENDIF. * * SELECT * INTO TABLE @gt_ztr2t0190 FROM ztr2t0190 * WHERE bank_ind = @bank_ind * AND bank_action = @bank_action. * SORT gt_ztr2t0190 BY bank_ind bank_action zeile. * * "针对承兑协议接口特殊处理 add by xxj * DELETE gt_ztr2t0190 WHERE parent IS INITIAL AND name IS INITIAL. * lt_ztr2t0190 = gt_ztr2t0190. * * LOOP AT lt_ztr2t0190 INTO DATA(ls_zrtr05450) . * READ TABLE gt_ztr2t0190 INTO DATA(temp_ztr2t0190) WITH KEY bank_ind = ls_zrtr05450-bank_ind * bank_action = ls_zrtr05450-bank_action * zeile = ls_zrtr05450-zeile. * IF sy-subrc = 0. * CHECK temp_ztr2t0190-zdele NE abap_true. * ENDIF. * lv_parent = ls_zrtr05450-parent. * lv_name = ls_zrtr05450-name. * IF ls_zrtr05450-attribute1 IS NOT INITIAL. * ls_attribute-fname = ls_zrtr05450-attribute1. * ls_attribute-fvalue = ls_zrtr05450-attribute_value1. * APPEND ls_attribute TO lt_attributes. * ENDIF. * IF ls_zrtr05450-attribute2 IS NOT INITIAL. * ls_attribute-fname = ls_zrtr05450-attribute2. * ls_attribute-fvalue = ls_zrtr05450-attribute_value2. * APPEND ls_attribute TO lt_attributes. * ENDIF. * IF ls_zrtr05450-attribute3 IS NOT INITIAL. * ls_attribute-fname = ls_zrtr05450-attribute3. * ls_attribute-fvalue = ls_zrtr05450-attribute_value3. * APPEND ls_attribute TO lt_attributes. * ENDIF. * IF ls_zrtr05450-xml_repeat IS INITIAL. * * SELECT * INTO TABLE @DATA(lt_ztr2t0190_tmp) FROM ztr2t0190 WHERE parent = @ls_zrtr05450-name AND bank_action = @bank_action AND bank_ind = @bank_ind. * IF sy-subrc = 0. * CLEAR lv_value. * IF lv_parent IS INITIAL. * IF bank_ind = 'BOC'. * lv_parent = 'trans'. * ELSEIF bank_ind = 'CCB'. * lv_parent = 'TX_INFO'. * ENDIF. * me->create_element( document = lr_document * parent = lv_parent * name = lv_name * value = lv_value * attributes = lt_attributes ). * ELSE. * ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE data TO <fs_value>. * IF sy-subrc NE 0. * ASSIGN ls_test TO <fs_value>. * ENDIF. * DATA(temp_gid) = me->create_element( document = lr_document * parent = lv_parent * name = lv_name * value = lv_value * attributes = lt_attributes * repeat = abap_true ). * me->create_element_struc( data = <fs_value> * parent = ls_zrtr05450-name * document = lr_document * bank_ind = bank_ind * bank_action = bank_action * repeat_gid = temp_gid ). * ENDIF. * ELSE. * ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE data TO <fs_value>. * IF sy-subrc = 0. * lv_value = <fs_value>. * IF lv_parent IS INITIAL. * IF bank_ind = 'BOC'. * lv_parent = 'trans'. * ELSEIF bank_ind = 'CCB'. * lv_parent = 'TX_INFO'. * ENDIF. * * ENDIF. * me->create_element( document = lr_document * parent = lv_parent * name = lv_name * value = lv_value * attributes = lt_attributes ). * ENDIF. * ENDIF. * * ELSE. * * ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE data TO <ft_table>. * * me->create_element_table( table = <ft_table> * document = lr_document * bank_ind = bank_ind * bank_action = bank_action * parent = ls_zrtr05450-name * grandparent = ls_zrtr05450-parent ). * * ENDIF. * CLEAR:lv_value,lv_parent,lt_attributes,ls_attribute. * * ENDLOOP. * * * CALL FUNCTION 'SDIXML_DOM_TO_XML' * EXPORTING * document = lr_document * IMPORTING * xml_as_string = lv_xml_as_string * size = lv_xml_size * TABLES * xml_as_table = lt_xml_as_table * EXCEPTIONS * no_document = 1 * OTHERS = 2. * e_xml = cl_proxy_service=>xstring2cstring( lv_xml_as_string ). * * IF bank_ind = 'CCB'. * REPLACE 'encoding="utf-8"' INTO e_xml WITH 'encoding="GB2312"'. * e_xml = 'requestXml=' && e_xml. * ENDIF. * * IF bank_ind = 'CBI' OR bank_ind = 'CITIC'. * REPLACE 'encoding="utf-8"' INTO e_xml WITH 'encoding="GBK"'. * ENDIF. ENDMETHOD. METHOD CONVERT_XML_TO_ABAP. DATA:ixml TYPE REF TO if_ixml. DATA:streamfactory TYPE REF TO if_ixml_stream_factory. DATA:istream TYPE REF TO if_ixml_istream. DATA:c_document TYPE REF TO cl_xml_document. DATA:lt_value TYPE TABLE OF type_s_key_value. DATA:parser TYPE REF TO if_ixml_parser, document TYPE REF TO if_ixml_document. * CLEAR:parser,document. ixml = cl_ixml=>create( ). streamfactory = ixml->create_stream_factory( ). istream = streamfactory->create_istream_string( string = response ). document = ixml->create_document( ). parser = ixml->create_parser( stream_factory = streamfactory istream = istream document = document ). IF parser->parse( ) <> 0. "准换xlm 错误 ENDIF. "XLM 处理 IF parser->is_dom_generating( ) = 'X'." ENDIF. me->transformation( EXPORTING bank_ind = bank_ind bank_action = bank_action document = document IMPORTING data = data ). ENDMETHOD. METHOD CREATE_DEFAULT_HEADER. * "中行 * TYPES:BEGIN OF TY_HEAD, * TERMID TYPE STRING, * TRNID TYPE STRING, * CUSTID TYPE STRING, * CUSOPR TYPE STRING, * TRNCOD TYPE STRING, * TOKEN TYPE STRING, * END OF TY_HEAD. * * DATA:BEGIN OF LS_BOCB2E, * HEAD TYPE TY_HEAD, * TRANS TYPE STRING, * END OF LS_BOCB2E. * "建行 * * DATA:BEGIN OF LS_CCBGB2, * REQUEST_SN TYPE STRING, * CUST_ID TYPE STRING, * USER_ID TYPE STRING, * PASSWORD TYPE STRING, * TX_CODE TYPE STRING, * LANGUAGE TYPE STRING, * "BODY * TXT TYPE STRING, * END OF LS_CCBGB2. * * * * DATA:LV_PARENT TYPE STRING, * LV_NAME TYPE STRING, * LV_VALUE TYPE STRING. * * DATA:LT_ATTRIBUTES TYPE TYPE_T_KEY_VALUE, * LS_ATTRIBUTE TYPE TYPE_S_KEY_VALUE. * * FIELD-SYMBOLS:<FS_VALUE> TYPE ANY. * * IF BANK_IND = 'BOC'."中行 * SELECT SINGLE TERMID * CUSTID * CUSOPR * INTO CORRESPONDING FIELDS OF LS_BOCB2E-HEAD * FROM ZTR2T0020 * WHERE BANK_IND = BANK_IND * AND BUKRS = BUKRS. * IF SY-SUBRC <> 0. * SELECT SINGLE TERMID * CUSTID * CUSOPR * INTO CORRESPONDING FIELDS OF LS_BOCB2E-HEAD * FROM ZTR2T0020 * WHERE BANK_IND = BANK_IND * AND BUKRS = ''. * ENDIF. * LS_BOCB2E-HEAD-TRNCOD = TRNCOD. * ELSEIF BANK_IND = 'CCB'."建行 * SELECT SINGLE CUSOPR AS USER_ID "操作员 * CUSTID AS CUST_ID "客户编号 * PASSWORD "密码 * INTO CORRESPONDING FIELDS OF LS_CCBGB2 * FROM ZTR2T0020 * WHERE BANK_IND = BANK_IND * AND BUKRS = BUKRS. * * IF SY-SUBRC <> 0. * SELECT SINGLE CUSOPR AS USER_ID "操作员 * CUSTID AS CUST_ID "客户编号 * PASSWORD "密码 * INTO CORRESPONDING FIELDS OF LS_CCBGB2 * FROM ZTR2T0020 * WHERE BANK_IND = BANK_IND * AND BUKRS = ''. * ENDIF. * CLEAR LS_CCBGB2-REQUEST_SN. * DATA:GETNUMBER TYPE CHAR16. * CALL FUNCTION 'NUMBER_GET_NEXT' * EXPORTING * NR_RANGE_NR = '01' "传出对象定义的序号代码 * OBJECT = 'ZTR2_SEQCC' * IMPORTING * NUMBER = GETNUMBER. * IF SY-SUBRC <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. * ENDIF. * " LS_CCBGB2-REQUEST_SN = SY-DATUM && SY-UZEIT && '01'."请求序列号 * LS_CCBGB2-REQUEST_SN = GETNUMBER."请求序列号 * LS_CCBGB2-TX_CODE = TRNCOD."交易请求码 * LS_CCBGB2-LANGUAGE = 'CN'."语言 * * ELSEIF BANK_IND = 'ICBC'."工行 * * ENDIF. * SELECT * INTO TABLE @GT_ZTR2T0190 FROM ZTR2T0190 * WHERE BANK_IND = @BANK_IND * AND BANK_ACTION = @BANK_ACTION. * SORT GT_ZTR2T0190 BY BANK_IND BANK_ACTION ZEILE. * * LOOP AT GT_ZTR2T0190 INTO DATA(LS_ZRTR05450). * LV_PARENT = LS_ZRTR05450-PARENT. * LV_NAME = LS_ZRTR05450-NAME. * IF LS_ZRTR05450-ATTRIBUTE1 IS NOT INITIAL. * LS_ATTRIBUTE-FNAME = LS_ZRTR05450-ATTRIBUTE1. * LS_ATTRIBUTE-FVALUE = LS_ZRTR05450-ATTRIBUTE_VALUE1. * APPEND LS_ATTRIBUTE TO LT_ATTRIBUTES. * ENDIF. * IF LS_ZRTR05450-ATTRIBUTE2 IS NOT INITIAL. * LS_ATTRIBUTE-FNAME = LS_ZRTR05450-ATTRIBUTE2. * LS_ATTRIBUTE-FVALUE = LS_ZRTR05450-ATTRIBUTE_VALUE2. * APPEND LS_ATTRIBUTE TO LT_ATTRIBUTES. * ENDIF. * IF LS_ZRTR05450-ATTRIBUTE3 IS NOT INITIAL. * LS_ATTRIBUTE-FNAME = LS_ZRTR05450-ATTRIBUTE3. * LS_ATTRIBUTE-FVALUE = LS_ZRTR05450-ATTRIBUTE_VALUE3. * APPEND LS_ATTRIBUTE TO LT_ATTRIBUTES. * ENDIF. * SELECT * INTO TABLE @DATA(LT_ZTR2T0190_TMP) FROM ZTR2T0190 WHERE PARENT = @LS_ZRTR05450-NAME AND BANK_ACTION = @BANK_ACTION AND BANK_IND = @BANK_IND. * IF SY-SUBRC = 0. * IF LV_PARENT IS INITIAL. * ME->CREATE_ELEMENT( DOCUMENT = DOCUMENT * PARENT = LV_PARENT * NAME = LV_NAME * VALUE = LV_VALUE * ATTRIBUTES = LT_ATTRIBUTES ). * ELSE. * IF BANK_IND = 'BOC'. * ASSIGN COMPONENT LS_ZRTR05450-NAME OF STRUCTURE LS_BOCB2E TO <FS_VALUE>. * DATA(TEMP_GID) = ME->CREATE_ELEMENT( DOCUMENT = DOCUMENT * PARENT = LV_PARENT * NAME = LV_NAME * VALUE = LV_VALUE * ATTRIBUTES = LT_ATTRIBUTES * REPEAT = ABAP_TRUE ). * ME->CREATE_ELEMENT_STRUC( DATA = <FS_VALUE> * PARENT = LS_ZRTR05450-NAME * DOCUMENT = DOCUMENT * BANK_IND = BANK_IND * BANK_ACTION = BANK_ACTION * REPEAT_GID = TEMP_GID ). * ELSEIF BANK_IND = 'CCB'. * ASSIGN COMPONENT LS_ZRTR05450-NAME OF STRUCTURE LS_CCBGB2 TO <FS_VALUE>. * TEMP_GID = ME->CREATE_ELEMENT( DOCUMENT = DOCUMENT * PARENT = LV_PARENT * NAME = LV_NAME * VALUE = LV_VALUE * ATTRIBUTES = LT_ATTRIBUTES * REPEAT = ABAP_TRUE ). * ME->CREATE_ELEMENT_STRUC( DATA = <FS_VALUE> * PARENT = LS_ZRTR05450-NAME * DOCUMENT = DOCUMENT * BANK_IND = BANK_IND * BANK_ACTION = BANK_ACTION * REPEAT_GID = TEMP_GID ). * * ENDIF. * * ENDIF. * * ELSE. * IF BANK_IND = 'BOC'. * IF LS_ZRTR05450-ZDELE IS NOT INITIAL. "ADD BY CRH 20200628 增加判断,打删除标记的不再创建节点 * CONTINUE. * ELSE. * ASSIGN COMPONENT LS_ZRTR05450-NAME OF STRUCTURE LS_BOCB2E TO <FS_VALUE>. * IF SY-SUBRC = 0. * LV_VALUE = <FS_VALUE>. * ENDIF. * ME->CREATE_ELEMENT( DOCUMENT = DOCUMENT * PARENT = LV_PARENT * NAME = LV_NAME * VALUE = LV_VALUE * ATTRIBUTES = LT_ATTRIBUTES ). * ENDIF. * ELSEIF BANK_IND = 'CCB'. * ASSIGN COMPONENT LS_ZRTR05450-NAME OF STRUCTURE LS_CCBGB2 TO <FS_VALUE>. * IF SY-SUBRC = 0. * LV_VALUE = <FS_VALUE>. * ENDIF. * ME->CREATE_ELEMENT( DOCUMENT = DOCUMENT * PARENT = LV_PARENT * NAME = LV_NAME * VALUE = LV_VALUE * ATTRIBUTES = LT_ATTRIBUTES ). * * ENDIF. * * ENDIF. * CLEAR:LV_VALUE,LV_PARENT,LT_ATTRIBUTES,LS_ATTRIBUTE. * ENDLOOP. ENDMETHOD. METHOD CREATE_ELEMENT. DATA: lv_value TYPE string, lr_parent TYPE REF TO if_ixml_element, lr_element TYPE REF TO if_ixml_element, lr_parent_node TYPE REF TO if_ixml_node. lv_value = value. CONDENSE lv_value. IF parent IS INITIAL."创建根节点 lr_element = document->create_element( name = name ). document->append_child( new_child = lr_element ). IF attributes IS NOT INITIAL. LOOP AT attributes INTO DATA(ls_attribute). lr_element->set_attribute( name = ls_attribute-fname value = ls_attribute-fvalue ). ENDLOOP. ENDIF. ELSE. * 添加子节点 lr_parent = document->find_from_name( name = parent ). IF sy-subrc = 0. lr_element = document->create_simple_element( name = name parent = lr_parent value = lv_value ). IF attributes IS NOT INITIAL. LOOP AT attributes INTO ls_attribute. lr_element->set_attribute( name = ls_attribute-fname value = ls_attribute-fvalue ). ENDLOOP. ENDIF. * 根据GID获取父节点 IF repeat_gid IS NOT INITIAL. lr_parent_node = document->find_from_gid( gid = repeat_gid ). * 根据GID添加子节点 lr_parent_node->append_child( new_child = lr_element ). ELSE. * 直接添加子节点 lr_parent->append_child( new_child = lr_element ). ENDIF. * 获取con_repeat的gid IF repeat = abap_true. CLEAR e_gid. e_gid = lr_element->get_gid( ). ENDIF. ENDIF. ENDIF. ENDMETHOD. METHOD CREATE_ELEMENT_STRUC. DATA:lt_ZTRTXML_ABAP TYPE TABLE OF ZTRTXML_ABAP, ls_ZTRTXML_ABAP TYPE ZTRTXML_ABAP. FIELD-SYMBOLS:<ft_table> TYPE ANY TABLE, <fs_table> TYPE any. DATA:lv_parent TYPE string, lv_name TYPE string, lv_value TYPE string. FIELD-SYMBOLS:<fs_value> TYPE any. FIELD-SYMBOLS:<fs_value1> TYPE any. DATA:lt_test TYPE TABLE OF demo_gtt, ls_test TYPE demo_gtt. DATA:lt_attributes TYPE type_t_key_value, ls_attribute TYPE type_s_key_value. ASSIGN data TO <fs_value>. SELECT * INTO TABLE lt_ZTRTXML_ABAP FROM ZTRTXML_ABAP WHERE Zaction = bank_action AND parent = parent. SORT lt_ZTRTXML_ABAP BY Zaction zeile. "ADD BY CRH LOOP AT lt_ZTRTXML_ABAP INTO DATA(ls_zrtr05450). lv_name = ls_zrtr05450-name. lv_parent = ls_zrtr05450-parent. IF ls_zrtr05450-attribute1 IS NOT INITIAL. ls_attribute-fname = ls_zrtr05450-attribute1. ls_attribute-fvalue = ls_zrtr05450-attribute_value1. APPEND ls_attribute TO lt_attributes. ENDIF. IF ls_zrtr05450-attribute2 IS NOT INITIAL. ls_attribute-fname = ls_zrtr05450-attribute2. ls_attribute-fvalue = ls_zrtr05450-attribute_value2. APPEND ls_attribute TO lt_attributes. ENDIF. IF ls_zrtr05450-attribute3 IS NOT INITIAL. ls_attribute-fname = ls_zrtr05450-attribute3. ls_attribute-fvalue = ls_zrtr05450-attribute_value3. APPEND ls_attribute TO lt_attributes. ENDIF. IF ls_zrtr05450-xml_repeat IS NOT INITIAL. ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE <fs_value> TO <ft_table>. IF sy-subrc = 0. me->create_element_table( table = <ft_table> document = document bank_ind = bank_ind bank_action = bank_action parent = ls_zrtr05450-name grandparent = ls_zrtr05450-parent ). ENDIF. ELSE. SELECT * INTO TABLE @DATA(lt_ZTRTXML_ABAP_tmp) FROM ZTRTXML_ABAP WHERE parent = @ls_zrtr05450-name AND Zaction = @bank_action. IF sy-subrc = 0. CLEAR lv_value. ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE <fs_value> TO <fs_value1>. IF sy-subrc NE 0. ASSIGN ls_test TO <fs_value1>. ENDIF. DATA(temp_gid) = me->create_element( document = document parent = lv_parent name = lv_name value = lv_value attributes = lt_attributes repeat_gid = repeat_gid repeat = abap_true ). me->create_element_struc( data = <fs_value1> parent = ls_zrtr05450-name document = document bank_ind = bank_ind bank_action = bank_action repeat_gid = temp_gid ). ELSE. ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE <fs_value> TO <fs_value1>. IF sy-subrc = 0. lv_value = <fs_value1>. ENDIF. me->create_element( document = document parent = lv_parent name = lv_name value = lv_value attributes = lt_attributes repeat_gid = repeat_gid ). ENDIF. ENDIF. CLEAR:lv_name,lv_value. * CLEAR:<fs_value1>,<ft_table>. ENDLOOP. LOOP AT GT_ZTRTXML_ABAP ASSIGNING FIELD-SYMBOL(<fs_ZTRTXML_ABAP>) WHERE PARENT = PARENT. <fs_ZTRTXML_ABAP>-zdele = abap_true. ENDLOOP. * DELETE gt_ZTRTXML_ABAP WHERE parent = parent. ENDMETHOD. METHOD create_element_table. DATA:lt_ZTRTXML_ABAP TYPE TABLE OF ZTRTXML_ABAP, ls_ZTRTXML_ABAP TYPE ZTRTXML_ABAP. FIELD-SYMBOLS:<ft_table> TYPE ANY TABLE, <ft_table1> TYPE ANY TABLE, <fs_table> TYPE any. DATA:lv_parent TYPE string, lv_name TYPE string, lv_value TYPE string. DATA:lt_test TYPE TABLE OF demo_gtt, ls_test TYPE demo_gtt. DATA:temp_ZTRTXML_ABAP TYPE TABLE OF ZTRTXML_ABAP. FIELD-SYMBOLS:<fs_value> TYPE any. DATA:lt_attributes TYPE type_t_key_value, ls_attribute TYPE type_s_key_value. DATA:lv_repeat_gid TYPE i. ASSIGN table TO <ft_table>. SELECT * INTO TABLE lt_ZTRTXML_ABAP FROM ZTRTXML_ABAP WHERE Zaction = bank_action AND parent = parent. SORT lt_ZTRTXML_ABAP BY Zaction zeile."ADD BY CRH temp_ZTRTXML_ABAP = gt_ZTRTXML_ABAP. LOOP AT <ft_table> ASSIGNING <fs_table>. lv_name = parent. lv_parent = grandparent. lv_repeat_gid = me->create_element( document = document parent = lv_parent name = lv_name repeat = abap_true ). gt_ZTRTXML_ABAP = temp_ZTRTXML_ABAP. LOOP AT lt_ZTRTXML_ABAP INTO DATA(ls_zrtr05450). lv_name = ls_zrtr05450-name. lv_parent = ls_zrtr05450-parent. IF ls_zrtr05450-attribute1 IS NOT INITIAL. ls_attribute-fname = ls_zrtr05450-attribute1. ls_attribute-fvalue = ls_zrtr05450-attribute_value1. APPEND ls_attribute TO lt_attributes. ENDIF. IF ls_zrtr05450-attribute2 IS NOT INITIAL. ls_attribute-fname = ls_zrtr05450-attribute2. ls_attribute-fvalue = ls_zrtr05450-attribute_value2. APPEND ls_attribute TO lt_attributes. ENDIF. IF ls_zrtr05450-attribute3 IS NOT INITIAL. ls_attribute-fname = ls_zrtr05450-attribute3. ls_attribute-fvalue = ls_zrtr05450-attribute_value3. APPEND ls_attribute TO lt_attributes. ENDIF. IF ls_zrtr05450-xml_repeat IS NOT INITIAL. ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE <fs_table> TO <ft_table1>. me->create_element_table( table = <ft_table1> document = document bank_ind = bank_ind bank_action = bank_action parent = ls_zrtr05450-name grandparent = ls_zrtr05450-parent ). ELSE. SELECT * INTO TABLE @DATA(lt_ZTRTXML_ABAP_tmp) FROM ZTRTXML_ABAP WHERE parent = @ls_zrtr05450-name AND Zaction = @bank_action. IF sy-subrc = 0. CLEAR lv_value. ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE <fs_table> TO <fs_value>. IF sy-subrc NE 0. ASSIGN ls_test TO <fs_value>. ENDIF. DATA(temp_repeat_gid) = me->create_element( document = document parent = lv_parent name = lv_name value = lv_value attributes = lt_attributes repeat = abap_true repeat_gid = lv_repeat_gid ). me->create_element_struc( data = <fs_value> parent = ls_zrtr05450-name document = document bank_ind = bank_ind bank_action = bank_action repeat_gid = temp_repeat_gid ). ELSE. ASSIGN COMPONENT ls_zrtr05450-name OF STRUCTURE <fs_table> TO <fs_value>. IF sy-subrc = 0. lv_value = <fs_value>. ENDIF. me->create_element( document = document parent = lv_parent name = lv_name value = lv_value attributes = lt_attributes repeat_gid = lv_repeat_gid ). ENDIF. ENDIF. CLEAR:lv_name,lv_value,lv_parent. * CLEAR:<fs_value>,<ft_table1>. ENDLOOP. ENDLOOP. LOOP AT gt_ZTRTXML_ABAP ASSIGNING FIELD-SYMBOL(<fs_ZTRTXML_ABAP>) WHERE parent = parent. <fs_ZTRTXML_ABAP>-zdele = abap_true. ENDLOOP. * DELETE gt_ZTRTXML_ABAP WHERE parent = parent. ENDMETHOD. METHOD TRANSFORMATION. DATA: node TYPE REF TO if_ixml_node, iterator TYPE REF TO if_ixml_node_iterator, nodemap TYPE REF TO if_ixml_named_node_map, node_parent TYPE REF TO if_ixml_node, attr TYPE REF TO if_ixml_node, name TYPE string, name1 TYPE string, prefix TYPE string, value TYPE string, indent TYPE i, count TYPE i, index TYPE i. FIELD-SYMBOLS <fs_value> TYPE any. FIELD-SYMBOLS <ft_table> TYPE ANY TABLE. * DATA:ls_key_value TYPE type_s_key_value. * * CLEAR:key_value[]. SELECT * INTO TABLE @DATA(lt_ZTRTXML_ABAP) FROM ZTRTXML_ABAP WHERE Zaction = @bank_action. SORT lt_ZTRTXML_ABAP BY ZEILE. node ?= document. CHECK NOT node IS INITIAL. IF node IS INITIAL. EXIT. ENDIF. "CREATE A NODE ITERATOR iterator = node->create_iterator( ). "GET CURRENT NODE node = iterator->get_next( ). "LOOP OVER ALL NODES WHILE NOT node IS INITIAL. * indent = node->get_height( ) * 2. * indent = indent + 20. indent = node->get_height( ). CASE node->get_type( ). WHEN if_ixml_node=>co_node_element." ELEMENT NODE name = node->get_name( ). value = node->get_value( ). READ TABLE lt_ZTRTXML_ABAP INTO DATA(ls_zrtr05430) WITH KEY name = name. IF sy-subrc = 0 AND ls_zrtr05430-xml_repeat = 'X'. ASSIGN COMPONENT name OF STRUCTURE data TO <ft_table>. IF sy-subrc = 0. transformation_table( EXPORTING bank_ind = bank_ind bank_action = bank_action repeat_name = name IMPORTING lines = DATA(lv_times) CHANGING node = node table = <ft_table> ). DO lv_times - 1 TIMES. node = iterator->get_next( )."获取下一个节点 ENDDO. ENDIF. ENDIF. WHEN if_ixml_node=>co_node_text OR if_ixml_node=>co_node_cdata_section."TEXT NODE value = node->get_value( ). node_parent = node->get_parent( ). name1 = node_parent->get_name( ). READ TABLE lt_ZTRTXML_ABAP INTO ls_zrtr05430 WITH KEY name = name1. IF sy-subrc = 0. ASSIGN COMPONENT name1 OF STRUCTURE data TO <fs_value>. IF sy-subrc = 0. <fs_value> = value. ENDIF. ENDIF. ENDCASE. node = iterator->get_next( )."获取下一个节点 ENDWHILE. ENDMETHOD. METHOD transformation_table. DATA:iterator TYPE REF TO if_ixml_node_iterator, nodemap TYPE REF TO if_ixml_named_node_map, node_parent TYPE REF TO if_ixml_node, attr TYPE REF TO if_ixml_node, child TYPE REF TO if_ixml_node, name TYPE string, name1 TYPE string, prefix TYPE string, value TYPE string, indent TYPE i, count TYPE i, index TYPE i. DATA: l_attributes TYPE REF TO if_ixml_named_node_map. DATA: l_name TYPE string, l_value TYPE string, l_fieldname TYPE string, l_fieldvalue TYPE string. DATA:first_node TYPE string. SELECT * INTO TABLE @DATA(lt_zrtrt05430) FROM ztrtxml_abap WHERE zaction = @bank_action AND parent = @repeat_name. CHECK lt_zrtrt05430 IS NOT INITIAL. SORT lt_zrtrt05430 BY zeile. FIELD-SYMBOLS:<ft_table> TYPE STANDARD TABLE, <fs_table> TYPE any, <fs_value> TYPE any. DATA:lr_line TYPE REF TO data. ASSIGN table TO <ft_table>. * CREATE DATA lr_line LIKE LINE OF <ft_table>. * ASSIGN lr_line->* TO <fs_table>. CLEAR lines. iterator = node->create_iterator( ). "GET CURRENT NODE node = iterator->get_next( ). "LOOP OVER ALL NODES WHILE NOT node IS INITIAL. * indent = node->get_height( ) * 2. * indent = indent + 20. indent = node->get_height( ). CASE node->get_type( ). WHEN if_ixml_node=>co_node_element." ELEMENT NODE name = node->get_name( ). value = node->get_value( ). IF name = repeat_name. APPEND INITIAL LINE TO <ft_table> ASSIGNING <fs_table>. ENDIF. *--------------------------------------------------------------------* CLEAR:l_fieldname,l_fieldvalue. l_attributes = node->get_attributes( ). IF l_attributes IS NOT INITIAL. DATA(l_count) = l_attributes->get_length( ). DO l_count TIMES. DATA(l_index) = sy-index - 1. DATA(l_attr) = l_attributes->get_item( l_index ). l_name = l_attr->get_name( ). l_value = l_attr->get_value( ). IF l_name = 'key' . l_fieldname = l_value. ELSEIF l_name = 'value'. l_fieldvalue = l_value. ENDIF. ENDDO. ENDIF. READ TABLE lt_zrtrt05430 INTO DATA(ls_zrtr05430) WITH KEY name = l_fieldname. IF sy-subrc = 0. ASSIGN COMPONENT l_fieldname OF STRUCTURE <fs_table> TO <fs_value>. IF sy-subrc = 0. <fs_value> = l_fieldvalue. ENDIF. ENDIF. *--------------------------------------------------------------------* READ TABLE lt_zrtrt05430 INTO ls_zrtr05430 WITH KEY name = name. IF sy-subrc = 0. * IF name = first_node. * APPEND INITIAL LINE TO <ft_table> ASSIGNING <fs_table>. * ENDIF. IF ls_zrtr05430-xml_repeat = 'X'. ASSIGN COMPONENT name OF STRUCTURE <fs_table> TO <ft_table>. IF sy-subrc = 0. me->transformation_table( EXPORTING bank_ind = bank_ind bank_action = bank_action repeat_name = name IMPORTING lines = DATA(lv_times) CHANGING node = node table = <ft_table> ). DO lv_times - 1 TIMES. node = iterator->get_next( )."获取下一个节点 ENDDO. ENDIF. ENDIF. ENDIF. WHEN if_ixml_node=>co_node_text OR if_ixml_node=>co_node_cdata_section."TEXT NODE value = node->get_value( ). node_parent = node->get_parent( ). name1 = node_parent->get_name( ). READ TABLE lt_zrtrt05430 INTO ls_zrtr05430 WITH KEY name = name1. IF sy-subrc = 0. * IF name = first_node. * APPEND INITIAL LINE TO <ft_table> ASSIGNING <fs_table>. * ENDIF. ASSIGN COMPONENT name1 OF STRUCTURE <fs_table> TO <fs_value>. IF sy-subrc = 0. <fs_value> = value. ENDIF. ENDIF. ENDCASE. ADD 1 TO lines. node = iterator->get_next( )."获取下一个节点 ENDWHILE. ENDMETHOD. ENDCLASS. |
在程序 中调用ZCL_TR2_XML的CONVERT_XML_TO_ABAP方法,把XML文件转换为ABAP的结构 数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
TYPES: BEGIN OF TY_elXmlProperty, BNKNBR TYPE STRING, BNKTYP TYPE STRING, BRDCIT TYPE STRING, BRDNBR TYPE string, END OF TY_elXmlProperty. TYPES: TT_elXmlProperty TYPE TABLE OF TY_elXmlProperty. DATA: BEGIN OF LS_OUT_DATA, elXmlProperty TYPE TT_elXmlProperty, END OF LS_OUT_DATA. DATA(LO_XML) = NEW ZCL_TR2_XML( ). LO_XML->CONVERT_XML_TO_ABAP( EXPORTING RESPONSE = LS_OUT-DATA BANK_IND = '' BANK_ACTION = 'A101' IMPORTING DATA = LS_OUT_DATA ). |