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.

datal_string type string.
constants strmaxlen type 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_xstringcl_bcs_convert=>string_to_xstringl_string ).

datal_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.
dataxml_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(attnode->get_attributes).

data(l_lenatt->get_length).

do l_len times.
  data(pchildatt->get_itemsy-index ).
  data(attrnamepchild->get_name).
  data(attrvaluepchild->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:

Copyright © 2013 ABAP SAP - AbapFox! Aprenda ABAP Definitivamente