Cuando tenemos una cadena xml y es necesario convertirla en un objeto, debemos analizar la cadena xml.
Al igual que los otros lenguajes en ABAP, tenemos interfaces ixml estándar para analizar y procesar el xml. Los pasos son los siguientes:
Interfaz IF_IXML (en realidad, esta es una fábrica): CL_ixml=>create() – me dará una instancia de Factory. Esta es la fábrica principal, que luego me dará instancias de otras fábricas.
Luego necesitamos crear un documento XML (la interfaz para esto es IF_IXML_DOCUMENT). Utilizar la instancia de fábrica obtenida anteriormente para crear el documento (……->CREATE_DOCUMENT).
Este documento debe ser analizado. Para esto necesitamos:
Instancia de documento en blanco (obtenido de la fábrica anterior)
Fábrica de transmisiones – IF_IXML_STREAM_FACTORY (…->CREATE_STREAM_FACTORY)
Stream: que luego apuntará al XML real que necesitamos analizar (….>…>CREATE_ISTREAM_STRING). No hay una fábrica separada para esto. STREAM FACTORY tiene un método que luego le ayudará a crear la transmisión. Es en este método que pasamos al XML que debe ser analizado.
Ahora tenemos que crear un analizador: IF_IXML_PARSER. La fábrica principal en sí se puede utilizar para crear el analizador (…….….->CREATE_PARSER: aquí tenemos que pasar Document, Stream Factory y las instancias de transmisión)
Una vez que la instancia de Parser esté lista, llame al método PARSE. A partir de entonces, utilizar la instancia STREAM y llamar al método CLOSE para cerrar la secuencia.
Para apuntar a un nodo, necesitamos una declaración de instancia del tipo de nodo: IF_IXMl_NODE
Ejemplo de xml: prueba.xml
<?xml version="1.0" encoding="utf-8"?>
<asx:abap xmlns:asx="http://www.sap.com/abapxml" version="1.0"><asx:values><DATA><ACTION><FIELD><NAME>VBAK-AUART</NAME><VALUE/><TYPE>GuiCTextField</TYPE></FIELD><FIELD><NAME>VBAK-VKORG</NAME><VALUE/><TYPE>GuiCTextField</TYPE></FIELD><FIELD><NAME>VBAK-VTWEG</NAME><VALUE/><TYPE>GuiCTextField</TYPE></FIELD><FIELD><NAME>VBAK-SPART</NAME><VALUE/><TYPE>GuiCTextField</TYPE></FIELD><FIELD><NAME>VBAK-VKBUR</NAME><VALUE/><TYPE>GuiCTextField</TYPE></FIELD><FIELD><NAME>VBAK-VKGRP</NAME><VALUE/><TYPE>GuiCTextField</TYPE></FIELD></ACTION><ACTION><CLICK><TYPE>GuiButton</TYPE><PROGRAM_NAME>SAPMV45A</PROGRAM_NAME><SCREEN_NO>101</SCREEN_NO><NAME>btn[0]</NAME><SEQUENCE_NUMBER></SEQUENCE_NUMBER><TEXT>Enter</TEXT><NODE_PATH></NODE_PATH><NODE_LEVEL></NODE_LEVEL><NODE_KEY></NODE_KEY><ID>/app/con[1]/ses[0]/wnd[0]/tbar[0]/btn[0]</ID><NODE_STATE></NODE_STATE><TABLE_TYPE></TABLE_TYPE><ROW_NUMBER></ROW_NUMBER></CLICK></ACTION></DATA></asx:values></asx:abap>
Código de informe:
Error al representar la macro ‘code’: valor inválido especificado para el parámetro ‘com.atlassian.confluence.ext.code.render.InvalidValueException’
****Data declaration
data : lif_ixml type ref to if_ixml ,
lif_ixml_document type ref to if_ixml_document ,
lif_ixml_stream_factory type ref to if_ixml_stream_factory ,
lif_ixml_istream type ref to if_ixml_istream ,
lif_ixml_parser type ref to if_ixml_parser ,
lif_ixml_parse_error type ref to if_ixml_parse_error ,
lif_ixml_node type ref to if_ixml_node,
node type ref to if_ixml_node.
data : lt_xml_error type dts_tty_xml_error ,
lt_xml_error_field type dts_tty_xml_error ,
lv_error_count type i ,
lv_index type i.
data : lif_ixml_node_list type ref to if_ixml_node_list ,
lif_ixml_node_iterator type ref to if_ixml_node_iterator ,
lif_ixml_node1 type ref to if_ixml_node ,
lif_ixml_node_list1 type ref to if_ixml_node_list ,
lif_ixml_node_iterator1 type ref to if_ixml_node_iterator ,
lv_type type i ,
lv_name type string ,
lv_xml type string,
lv_value type string .
****Upload the attached text xml into variable lv_xml
****Show the xml which will be Parsed
cl_abap_browser=>show_xml(
exporting
xml_string = lv_xml " XML in String
).
**-- Create the Main Factory
lif_ixml = cl_ixml=>create( ).
**-- Create the Initial Document
lif_ixml_document = lif_ixml->create_document( ).
**-- Create a Stream Factory
lif_ixml_stream_factory = lif_ixml->create_stream_factory( ).
**-- Create an Input Stream
lif_ixml_istream = lif_ixml_stream_factory->create_istream_string( string = lv_xml ).
**-- Create a Parser
lif_ixml_parser = lif_ixml->create_parser(
document = lif_ixml_document
istream = lif_ixml_istream
stream_factory = lif_ixml_stream_factory
).
**-- check errors in parsing
if lif_ixml_parser->parse( ) <> 0.
if lif_ixml_parser->num_errors( ) <> 0.
lv_error_count = lif_ixml_parser->num_errors( ).
lv_index = 0.
while lv_index < lv_error_count.
lif_ixml_parse_error = lif_ixml_parser->get_error( index = lv_index ).
append initial line to lt_xml_error assigning <fs_xml_error>.
<fs_xml_error>column_name = lif_ixml_parse_error>get_column( ).
<fs_xml_error>line = lif_ixml_parse_error>get_line( ).
<fs_xml_error>reason = lif_ixml_parse_error>get_reason( ).
lv_index = lv_index + 1.
endwhile.
endif.
endif.
**-- Close the Input Stream after Parsing
lif_ixml_istream->close( ).
***Nevigate to the 'DATA' node of xml
lif_ixml_node = lif_ixml_document->find_from_name( name = 'DATA' ).
***Get name of the node name will be DATA
lv_name = lif_ixml_node->get_name( ).
***In order to process childs of DATA node we need to create iterator using its childerns
lif_ixml_node_list = lif_ixml_node->get_children( ).
lif_ixml_node_iterator = lif_ixml_node_list->create_iterator( ).
***Loop on actions
while lif_ixml_node is not initial.
lv_name = lif_ixml_node->get_name( ).
IF lif_ixml_node->get_name( ) = 'ACTION'.
lif_ixml_node1 = lif_ixml_node->get_first_child( ).
lif_ixml_node_list1 = lif_ixml_node->get_children( ).
lif_ixml_node_iterator1 = lif_ixml_node_list1->create_iterator( ).
***Loop at fields
WHILE lif_ixml_node1 is NOT INITIAL.
lv_name = lif_ixml_node1->get_name( ).
****element node
node = lif_ixml_node1->get_first_child( ).
IF node->get_name( ) = 'NAME'.
IF node->get_value( ) = 'VBAK-AUART'.
node = node->get_next( ).
node->set_value( value = 'Test1' ).
ENDIF.
IF node->get_value( ) = 'VBAK-VKORG'.
node = node->get_next( ).
node->set_value( value = 'Test2' ).
ENDIF.
IF node->get_value( ) = 'VBAK-VTWEG'.
node = node->get_next( ).
node->set_value( value = 'Test3' ).
ENDIF.
IF node->get_value( ) = 'VBAK-SPART'.
node = node->get_next( ).
node->set_value( value = 'Test4' ).
ENDIF.
IF node->get_value( ) = 'VBAK-VKBUR'.
node = node->get_next( ).
node->set_value( value = 'Test5' ).
ENDIF.
IF node->get_value( ) = 'VBAK-VKGRP'.
node = node->get_next( ).
node->set_value( value = 'Test6' ).
ENDIF.
IF node->get_value( ) = 'VBAK-AUGRU'.
node = node->get_next( ).
node->set_value( value = 'Test7' ).
ENDIF.
endif.
***Get the next field under action
lif_ixml_node1 = lif_ixml_node_iterator1->get_next( ).
ENDWHILE.
ENDIF.
***Set the node to next node
lif_ixml_node = lif_ixml_node_iterator->get_next( ).
endwhile.
***To show the xml on selection screen
*call function 'SDIXML_DOM_TO_SCREEN'
* exporting
* document = lif_ixml_document.
data : xml TYPE XSTRING.
call function 'SDIXML_DOM_TO_XML'
exporting
document = lif_ixml_document
IMPORTING
XML_AS_STRING = xml
.
CL_ABAP_BROWSER=>show_xml(
exporting
xml_xstring = xml " XML in XString
).
Dejanos tu comentario:
Ve a la página completa para ver y enviar el formulario.
Te puede interesar:
Últimas entradas
- Analizar un archivo xml e insertar valores en ABAP
- Introducción a SAP WORKFLOW
- Crear formularios con adobe forms
- Crear formularios con Print WorkBench
- SAP WORKFLOW – Estructura organizativa
Etiquetas
7.4 7.40 abap abap.ar adobe forms Ampliaciones analítica archivo atributos BADI Calculos capacitaciones CASE consultas contacto cursos datos Declaraciones enhancement Estructura Formularios HANA INNER introducción JOIN LIBROS manual MIGO OPEN organizativa print SAP sentencia sql tipos Vista workbench WORKFLOW XML