Skip to Content
0
Sep 21, 2023 at 01:09 PM

Reading data from XML node into internal table

149 Views Last edit Sep 21, 2023 at 01:14 PM 2 rev

Hello everyone,

I created a program that is collecting some XMLs via API. I am getting those XMLs as strings Now I need to extract some data from those XMLs.

This is how my program looks like:

REPORT z_testapi.

TRY.
DATA: lo_http_client TYPE REF TO if_http_client,
lo_rest_client TYPE REF TO cl_rest_http_client,
lo_response TYPE REF TO if_rest_entity,
lo_request TYPE REF TO if_rest_entity,
lv_url TYPE string,
http_status TYPE string,
response TYPE string,
lv_status TYPE i.

lv_url = 'https://nothing.com:8090/api/Publicapi/sales-invoice/xml?invoiceId=1234567'.

cl_http_client=>create_by_url( EXPORTING
url = lv_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).

IF sy-subrc <> 0.
lo_http_client->close( ).
ENDIF.

IF lo_http_client IS BOUND.
lo_http_client->request->set_method( if_http_request=>co_request_method_get ).

lo_http_client->request->set_header_field(
name = 'accept' " Header Name
value = 'text/plain' ). " Header Value

lo_http_client->request->set_header_field(
name = 'ApiKey' " Header Name
value = '6532134-4f33d-43fer-fef3-123456789098' ). " Header Value

lo_http_client->send( timeout = if_http_client=>co_timeout_default ).

lo_http_client->receive( ).

lo_http_client->response->get_status( IMPORTING
code = lv_status
reason = http_status ).

WRITE: / 'HTTP STATUS CODE:', lv_status.
WRITE: /'STATUS_TEXT:', http_status.

IF lv_status = 200.
DATA(lv_result) = lo_http_client->response->get_cdata( ).
WRITE: /'Response:'.
WRITE: / lv_result.
ENDIF.

TYPES: BEGIN OF ts_record,
code TYPE string,
isonshortlist TYPE string,
nameeng TYPE string,
namesrbcyr TYPE string,
namesrbltn TYPE string,
symbol TYPE string,
END OF ts_record.
DATA: lt_act TYPE SORTED TABLE OF ts_record WITH UNIQUE KEY code.
DATA: lv_xstring TYPE xstring.
DATA: gt_xml_data TYPE TABLE OF smum_xmltb.
DATA: wa_xml_data LIKE LINE OF gt_xml_data.
DATA: gt_konacna TYPE TABLE OF smum_xmltb.
DATA: gt_return TYPE TABLE OF bapiret2.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = lv_result
* MIMETYPE = ' '
* ENCODING =
IMPORTING
buffer = lv_xstring
* EXCEPTIONS
* FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

CALL FUNCTION 'SMUM_XML_PARSE'
EXPORTING
xml_input = lv_xstring
TABLES
xml_table = gt_xml_data
return = gt_return.


lo_http_client->close( ).

ENDIF.

CATCH cx_root INTO DATA(e_txt).
WRITE: / e_txt->get_text( ).

ENDTRY.
This is example of the XML that I have:
<?xml version="1.0" encoding="utf-8"?>
<env:DocumentEnvelope xmlns:env="urn:eFaktura:MinFinrs:envelop:schema">
  <env:DocumentHeader>
    <env:SalesInvoiceId>2445171</env:SalesInvoiceId>
    <env:PurchaseInvoiceId>2355569</env:PurchaseInvoiceId>
    <env:DocumentId>5f7e849d-4b97-4a4a-a640-56faf4b34414</env:DocumentId>
    <env:CreationDate>2023-09-13</env:CreationDate>
    <env:SendingDate>2023-09-13</env:SendingDate>
      </env:DocumentHeader>
  <env:DocumentBody>
    <Invoice xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sbt="http://mfin.gov.rs/srbdt/srbdtext" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2">
      <cbc:CustomizationID>urn:cen.eu:en16931:2017#compliant#urn:mfin.gov.rs:srbdt:2021</cbc:CustomizationID>
      <cbc:ID>2762</cbc:ID>
      <cbc:IssueDate>2023-09-13</cbc:IssueDate>
      <cbc:DueDate>2023-09-13</cbc:DueDate>
      <cbc:InvoiceTypeCode>380</cbc:InvoiceTypeCode>
      <cbc:DocumentCurrencyCode>RSD</cbc:DocumentCurrencyCode>
      <cac:InvoicePeriod>
        <cbc:DescriptionCode>3</cbc:DescriptionCode>
      </cac:InvoicePeriod>
      <cac:AccountingSupplierParty>
        <cac:Party>
          <cbc:EndpointID schemeID="9948">111111111</cbc:EndpointID>
          <cac:PartyIdentification>
            <cbc:ID>12345678</cbc:ID>
          </cac:PartyIdentification>
          <cac:PartyName>
            <cbc:Name>Leaving</cbc:Name>
          </cac:PartyName>
          <cac:PostalAddress>
            <cbc:StreetName>Not important 7</cbc:StreetName>
            <cbc:CityName>Nowhere</cbc:CityName>
            <cbc:PostalZone>132000</cbc:PostalZone>
            <cac:Country>
              <cbc:IdentificationCode>BB</cbc:IdentificationCode>
              <cbc:Name>Nowhereland</cbc:Name>
            </cac:Country>
          </cac:PostalAddress>
          <cac:PartyTaxScheme>
            <cbc:CompanyID>RS111111111</cbc:CompanyID>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:PartyTaxScheme>
          <cac:PartyLegalEntity>
            <cbc:RegistrationName>Leaving</cbc:RegistrationName>
            <cbc:CompanyID>12345678</cbc:CompanyID>
          </cac:PartyLegalEntity>
        </cac:Party>
      </cac:AccountingSupplierParty>
      <cac:AccountingCustomerParty>
        <cac:Party>
          <cbc:EndpointID schemeID="9948">11112222</cbc:EndpointID>
          <cac:PartyIdentification>
            <cbc:ID>87654321</cbc:ID>
          </cac:PartyIdentification>
          <cac:PartyName>
            <cbc:Name>MARKET</cbc:Name>
          </cac:PartyName>
          <cac:PostalAddress>
            <cac:Country>
              <cbc:IdentificationCode>RS</cbc:IdentificationCode>
            </cac:Country>
          </cac:PostalAddress>
          <cac:PartyTaxScheme>
            <cbc:CompanyID>RS11112222</cbc:CompanyID>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:PartyTaxScheme>
          <cac:PartyLegalEntity>
            <cbc:RegistrationName>MARKET</cbc:RegistrationName>
            <cbc:CompanyID>87654321</cbc:CompanyID>
          </cac:PartyLegalEntity>
        </cac:Party>
      </cac:AccountingCustomerParty>
      <cac:Delivery>
        <cbc:ActualDeliveryDate>2023-09-13</cbc:ActualDeliveryDate>
      </cac:Delivery>
      <cac:PaymentMeans>
        <cbc:PaymentMeansCode>30</cbc:PaymentMeansCode>
        <cac:PayeeFinancialAccount>
          <cbc:ID>170000030022602031</cbc:ID>
        </cac:PayeeFinancialAccount>
      </cac:PaymentMeans>
      <cac:TaxTotal>
        <cbc:TaxAmount currencyID="RSD">11331.20</cbc:TaxAmount>
        <cac:TaxSubtotal>
          <cbc:TaxableAmount currencyID="RSD">56656.00</cbc:TaxableAmount>
          <cbc:TaxAmount currencyID="RSD">11331.20</cbc:TaxAmount>
          <cac:TaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>20</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:TaxCategory>
        </cac:TaxSubtotal>
      </cac:TaxTotal>
      <cac:LegalMonetaryTotal>
        <cbc:LineExtensionAmount currencyID="RSD">56656.00</cbc:LineExtensionAmount>
        <cbc:TaxExclusiveAmount currencyID="RSD">56656.00</cbc:TaxExclusiveAmount>
        <cbc:TaxInclusiveAmount currencyID="RSD">67987.20</cbc:TaxInclusiveAmount>
        <cbc:AllowanceTotalAmount currencyID="RSD">0</cbc:AllowanceTotalAmount>
        <cbc:PrepaidAmount currencyID="RSD">0</cbc:PrepaidAmount>
        <cbc:PayableAmount currencyID="RSD">67987.20</cbc:PayableAmount>
      </cac:LegalMonetaryTotal>
      <cac:InvoiceLine>
        <cbc:ID>1</cbc:ID>
        <cbc:InvoicedQuantity unitCode="H87" unitCodeListID="H87">1</cbc:InvoicedQuantity>
        <cbc:LineExtensionAmount currencyID="RSD">56656.00</cbc:LineExtensionAmount>
        <cac:Item>
          <cbc:Name>MaterialNo1</cbc:Name>
          <cac:ClassifiedTaxCategory>
            <cbc:ID>S</cbc:ID>
            <cbc:Percent>20</cbc:Percent>
            <cac:TaxScheme>
              <cbc:ID>VAT</cbc:ID>
            </cac:TaxScheme>
          </cac:ClassifiedTaxCategory>
        </cac:Item>
        <cac:Price>
          <cbc:PriceAmount currencyID="RSD">56656</cbc:PriceAmount>
        </cac:Price>
      </cac:InvoiceLine>
    </Invoice>
  </env:DocumentBody>
</env:DocumentEnvelope><br>

It contains Invoice data. Can someone help me with how can I extract only certain data from this XML and put that data into some internal table that will have 2 columns (Name and Value).

For example I need only data from these 2 nodes:

<cbc:ID>2762</cbc:ID>
...
 <cbc:LineExtensionAmount currencyID="RSD">56656.00</cbc:LineExtensionAmount>

Best regards to anyone reading this,
hoping for some help.