简体   繁体   中英

Handling data modification in SAP from web service in XML

I am developing a creation/modification web service in SAP. Creation is OK but for modification the calling system has a standard:

  • if they send a tag that's filled with a value: modify field in SAP
  • if the tag is empty: clear field in SAP
  • if any tag is not present: do nothing to the corresponding field

In SAP, I am in a class and see a simple structure of fields. No view of the XML. In addition, between me and other system is SAP-PI, a middleware. Apparently, PI always sends all tags. So how do I detect the three states?

For now our solution is PI sends me all tags and for those that he detects as "present" he adds an attribute is_present='X' to the tag. With this all the fields become structures with two fields: CONTENT, IS_PRESENT.

Before I rework my code, does anybody know if there is another way to do this?

There is a best practice to recognize whether a field is blank because its value is blank or the corresponding XML tag is missing.

Precondition: PI must not arbitrarily add missing tags

Because

Apparently, PI always sends all tags

is generally false. I don't know if your PI mapping adds missing tags for some reason but generally speaking PI definitely has the ability keep a tag absent in the xml resulted after the conversion.

If your PI mapping must add those missing tags or you can't change it, you can stop reading further.

Assuming you have the following situation:

[Source xml] -> PI -> [Converted xml] -> ECC or S/4

And source xml contains

<root>
<field1>changed value</field1>
<field2></field2> <!-- value to be cleared -->
<!--
    <field3> is missing in this particular message
-->
</root>

The message resulting after the PI conversion should be like

<root_proxy>
<field1>changed value</field1>
<field2>value to be cleared</field2>
<!--
    <field3> is missing in this particular message
-->
</root_proxy>

And not

<root_proxy>
<field1>changed value</field1>
<field2></field2> <!-- value to be cleared -->
<field3></field3> <!-- tag added by PI -->
</root_proxy>

At Backend side

In the input data of your inbound method you'll find some fields called CONTEXT ( PRXCTRLTAB type). You have one CONTEXT for every structure, in case of deep structures.

In this table you can find the field names where XML is missing. Example:

loop at input-root_proxy-context into data(ls_context). " cycle through all input fields
  case ls_context-value.
  when sai_ctrl_none.    " handle missing xml tag
  when sai_ctrl_initial. " handle fields with initial value
  endcase.
endloop.

To activate this feature implement the CONSTRUCTOR of your ABAP Proxy class and set SET_EXTENDED_XML_HANDLING = 'X' . For example:

DATA:  
  lo_context   TYPE REF TO if_ws_server_context,
  lo_protocol TYPE REF TO if_wsprotocol_payload.

lo_context   = cl_proxy_access=>get_server_context( ).
lo_protocol = lo_context->get_protocol( if_wsprotocol=>payload ).
lo_protocol->set_extended_xml_handling( abap_true ).

Source: Activating Extended XML Handling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM