cancel
Showing results for 
Search instead for 
Did you mean: 

Reading data from XML node into internal table

miske
Participant
0 Kudos

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.

Sandra_Rossi
Active Contributor
0 Kudos

Non-released API is not API (SMUM_XML_PARSE).

Released API is for instance IXML, SXML, ST, XSLT.

Anyway, if you still want to use SMUM_XML_PARSE, you forgot to paste the contents of the results, and forgot to say what your issue is.

miske
Participant
0 Kudos

Hello sandra.rossi ,
thanks for respoding. Lv_result is containing the XML string that I posted.After calling FM SMUM_XML_PARSE table gt_xml_data gets filled with data from that XML. Gt_xml_data has 4 colums (HIER, TYPE, CNAME, CVALUE) the first one is cotaining the number in the XML hierarchy for every node, 2nd one is the type (A - attribute and V - value), 3rd one is name of the node and 4th one the value if any . The main issue is that there are nodes that have the same name but are position in different parts of the XML. I need to get some data from certain nodes Depending on the where in the XML the node is placed, For example node <cbc:Name> is appearing more than once but I would like to get a particular one. (for example

env:DocumentBodyInvoice -> cac:AccountingSupplierParty -> cac:PartyName -> <cbc:Name>Leaving</cbc:Name> )

Maybe there is a way for me to get the value in this Name node using its parent nodes? I know that this will always be the "path" to this node.

I am not sure if I am being clear since I am new with this. If you need any more information to help me, please ask.

Thanks.

raymond_giuseppi
Active Contributor
0 Kudos

Did you look at SAP standard similar tools such as CL_EDOCUMEN_XX->GET_ATTACHMENTS (XX for your country code)

Sandra_Rossi
Active Contributor
0 Kudos

I know how SMUM_XML_PARSE works (and its limitations), I'm just asking the values you obtain to clarify why you are asking the question i.e. to illustrate "The main issue is that there are nodes that have the same name but are position in different parts of the XML [...] node <cbc:Name> is appearing more than once but I would like to get a particular one (for example env:DocumentBodyInvoice -> cac:AccountingSupplierParty -> cac:PartyName -> <cbc:Name>Leaving</cbc:Name> )"

It's essential information so it would better appear in the question, possibly with screenshot(s) to make it more clear.

If you want, XPath is part of XSLT and can be used to address nodes directly. You may also use ST and IXML for that. Also SXML but I guess it would be less "comfortable" for what you want to do.

If you use XSLT, it would be something like this:

<xsl:value-of select="/env:DocumentBodyInvoice/cac:AccountingSupplierParty/cac:PartyName/cbc:Name"/>

If you use ST, it would be something like this:

<env:DocumentBodyInvoice>
  <cac:AccountingSupplierParty>
    <cac:PartyName>
      <cbc:Name>
        <tt:value ref=".ROOT"/>        

If you use IXML, it would be something like this:

ixml->find_from_path_ns( path = '/"https://www.env.com/:DocumentBodyInvoice"/"https://www.cac.com/:AccountingSupplierParty"' ).

etc.

Accepted Solutions (0)

Answers (0)