Abap~SAP Forma simples de alterar um atributo ou valor em um nó no XML
Uma coisa que as vezes parece simples fica bem complexa no ABAP. Depois de varias tentativas, encontrei uma forma bem fofinha de atualizar um atributo ou valor de uma TAG no XML.
*&---------------------------------------------------------------------*
*& Report YRB_CHANGE_XML
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report yrb_change_xml.
data: l_string type string.
constants strmaxlen type i value 255.
concatenate
* '<?xml version="1.0" encoding="utf-8"?>'
'<asx:abap version="1.0" xmlns:asx="http://www.sap.com/abapxml">'
' <asx:values>'
' <DATA href="#o9"/>'
' </asx:values>'
'<asx:heap xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:abap="http://www.sap.com/abapxml/types/built-in" xmlns:cls="http://www.sap.com/abapxml/classes/global" xmlns:dic="http://www.sap.com/abapxml/types/dictionary">'
' <prg:LCL_DATA id="o9" xmlns:prg="http://www.sap.com/abapxml/classes/program/YRB_TEXT_XML01">'
' <local.LCL_DATA>'
' <M_NAME>Creme</M_NAME>'
' </local.LCL_DATA>'
' </prg:LCL_DATA>'
' </asx:heap>'
'</asx:abap>' into l_string.
data(l_xstring) = cl_bcs_convert=>string_to_xstring( l_string ).
data: l_xml type ref to cl_xml_document,
if_xml type ref to if_ixml_document,
node type ref to if_ixml_node,
rval type string.
data: xml_out type string.
create object l_xml.
call method l_xml->parse_xstring
exporting
stream = l_xstring.
node = l_xml->find_node( '/asx:abap/asx:heap/prg:LCL_DATA' ).
check node is not initial.
data(att) = node->get_attributes( ).
data(l_len) = att->get_length( ).
do l_len times.
data(pchild) = att->get_item( sy-index - 1 ).
data(attrname) = pchild->get_name( ).
data(attrvalue) = pchild->get_value( ).
if attrname = 'prg'.
pchild->set_value( |http://www.sap.com/abapxml/classes/program/{ sy-repid }| ).
endif.
enddo.
l_xml->render_2_xstring(
importing
stream = l_xstring
).
0 comentários: