Since XML is such a happening topic, theĀ CL_XML_DOCUMENT
class should be in the picture. I've tried using this class a few times and it has proven to be a difficult task. The iXML
setup is newer, more effective and has working examples on the system.
A class dedicated to interpreting and composing XML documents. Walk through nodes, transform from DOM (Document Object Model, the internal representation of an XML file) to XML-stream (essentialy a string) and vice-versa. If transformations are not used, this class can give you more detailed control. Try transformations first though.
The transformation will switch between representation of Abap structured data to XML structured data and vice versa. You will find that if the XML data to be interpreted is not known before-hand on SAP side, transformations don't work. But again: normally you will know what's coming in your XML file and a transformation is the way to get it out.
The CL_XML_DOCUMENT
class (1999)
The CL_XML_DOCUMENTĀ
class can be used to handle an XML file. Let's assume a file is presented to us and we want to pick it up and do some tricks. First of all, define the class and read the file:
DATA: lo_xml TYPE REF TO cl_xml_document, lv_retcode TYPE sy-subrc. CREATE OBJECT lo_xml. TRY. lv_retcode = lo_xml->import_from_file( EXPORTING filename = 'C:/temp/my_xml_file.xml' ). CATCH lcx_xml_error INTO ex. MESSAGE ex->error TYPE 'E'. ENDTRY.
Now we will add a bit of logic to read a section of data into a table:
DATA: lw_mara type mara. lo_xml->get_data( exporting name = 'TAGFORMARA' importing retcode = lv_retcode changing dataobject = lw_mara ).
A document can be read or composed by altering content, adding nodes or deleting them. There is a method to look up a node FIND_NODE
which will return a reference to class (interface) TYPE REF TO IF_IXML_NODE
which holds another impressive set of methods. And after all is said and done, just render your document into a string again (method RENDER_2_STRING
).
iXML class CL_IXML
There is also a newer setup to support XML matters, with methods to set up a parser and a renderer and even with example programs to demonstrate. It doesn't make handling XML files any easier. These tools are set up to cater all there is to cater and it loses the clarity and uniformity an actual XML file does have. I'm afraid I have to say: SAP has got it wrong here - these classes should make reading / interpreting an XML file much easier. For creating such file the TRANSFORMATION does a wonderful job, but for reading / interpreting, a different story alltogether.
There is an easy to use and very basic LCL_EASY_XML
parser on AbapcadabrA, you may want to check it out !