Using XML in an ABAP Project

Extensible Markup Language (XML) is a meta markup language that is used to define structured documents that can be easily exchanged between heterogeneous systems.


There are many ways to exchange data between systems, but with the growth of web services, XML has gained popularity among developers. The beauty of XML lies in its flexibility and simplicity.


XML defines a standard that can be used to define the format of documents. Using XML, you can structure and organize various kinds of data. A markup language (e.g., HTML) consists of predefined tags that allow you to format and organize the data. Tags are maintained between less than (<) and greater than (>) symbols. For example, in an HTML document, you can open the paragraph tag with <p> and close it with </p>. The content maintained between the tags is interpreted accordingly. Here, the content maintained between <p> and </p> is formatted as a paragraph; the content maintained between <h1> and </h1> is interpreted as a header.


However, XML is a meta markup language, which defines a markup language. It’s designed to be flexible and easy to understand for both humans and machines. In XML, there are no predefined tags; any tag can be used to describe content.


Even though an in-depth understanding of XML documents and their processing is beyond the scope of this post, we will discuss the basic XML syntax and look at the iXML library provided by SAP to create XML documents in ABAP. Understanding XML is helpful when exposing or consuming data with external applications. This post should help you appreciate how OOP aids in designing libraries that are easy for developers to use in their programs.


XML Overview

XML files provide great flexibility to share structured documents. Because there are no predefined tags, you can use tags that suit your domain and that are self-explanatory. Of course, developing a standard for document markup makes it easy for third-party vendors to develop software that can process XML files.


For example, say we have a website that can be extended by installing custom extensions. The extensions are of two types: components and plug-ins (i.e., the website can be extended by developing a component or a plug-in). We’ve developed a component and want to upload the files of the custom component as a package to the web server. To provide the information about the component to the installer script, we can supply an XML file with the package that contains information about the file structure of the package, as shown in the listing below. The installer can read this XML file and upload the files in the package to the designated folders in the web server.


<?xml version="1.0" encoding="utf-8"?>

<!—This is comment-->

<extension type="component">

   <files folder="site">




   <media folder="media">







As shown above, XML documents are organized into a series of elements. The first line in the document specifies the XML version and the encoding used. The syntax for this statement is <?xml version="1.0" encoding=""?>, and it’s optional.


The basic syntax to define an element in XML is as follows:


<element_name attribute_name=attribute_value>

   <!-- Element Content -->



In the example XML file provided, for the <extension type="component"> tag, the element name is extension, the attribute name is type, and the attribute value is component. This tells the upload script that we’re trying to upload an extension that’s a component. Tags <files> and <media> are the elements under parent node <extension>. The tags under <files> and <media> are the respective elements’ content, which specifies the folders and files that we’re uploading. The document is closed with closing tag </extension>. Comments can be maintained between<!-- and -->.


XML markup is case-sensitive; for example, the element name <ELEMENT> is different from <element>.


XML Processing Concepts

SAP NetWeaver AS for ABAP provides an iXML library that can be used to process XML files. This library implements various interfaces that allow you to work with XML files by calling various methods.


The listing below shows the code to create an XML file using the iXML library. The XML file we’re creating contains the data from the listing shown earlier. In this example, we’re defining reference objects for the XML document from the iXML library. The reference object for the XML document is referenced to interface if_ixml_document, and a reference object is defined for each element in the XML file by referencing if_ixml_element. The XML file is generated by calling the respective methods from the library, as shown here.



*Declarations to create XML document

DATA: lr_ixml TYPE REF TO if_ixml. "Reference for iXML object

"Reference for XML document

DATA: lr_document TYPE REF TO if_ixml_document.

."Reference for "extension" element in document

DATA: lr_extension TYPE REF TO if_ixml_element


."Reference for "files" element in document

DATA: lr_files TYPE REF TO if_ixml_element

."Reference for "media" element in document

DATA: lr_media TYPE REF TO if_ixml_element

."Reference to set encoding

DATA: lr_encoding TYPE REF TO if_ixml_encoding


*Declarations to create output stream and render the file to

*application server directory

DATA: lr_streamfactory TYPE REF TO if_ixml_stream_factory,

lr_ostream TYPE REF TO if_ixml_ostream,

lr_renderer TYPE REF TO if_ixml_renderer.



* Create iXML object

lr_ixml = cl_ixml=>create( ).

lr_document = lr_ixml->create_document( ).

*Create encoding

lr_encoding = lr_ixml->create_encoding( BYTE_ORDER = 0 CHARACTER_SET = 'UTF-8').

*Set encoding

lr_document->set_encoding( lr_encoding ).

*Create element "extension" as root

lr_extension = lr_document->create_simple_element(

name = 'extension'

parent = lr_document ).

*Set attribute for the "extension" element

lr_extension->set_attribute( name = 'Type'

VALUE = 'Component' ).

*Create "files" element with "extension" element as parent

lr_files = lr_document->create_simple_element(

name = 'files'

parent = lr_extension ).

*Set attribute for "files" element

lr_files->set_attribute( name = 'Folder'

VALUE = 'site' ).

*Create element content

lr_document->create_simple_element( name = 'filename'

parent = lr_files

VALUE = 'index.html' ).

lr_document->create_simple_element( name = 'filename'

parent = lr_files

VALUE = 'site.php' ).


*Create "media" element with "extension" element as parent

lr_media = lr_document->create_simple_element(

name = 'media'

parent = lr_extension ).

**Set attribute for "media" element

lr_media->set_attribute( name = 'Folder'

VALUE = 'media' ).


*Create element content

lr_document->create_simple_element( name = 'folder'

parent = lr_media

VALUE = 'css' ).

lr_document->create_simple_element( name = 'folder'

parent = lr_media

VALUE = 'images' ).

lr_document->create_simple_element( name = 'folder'

parent = lr_media

VALUE = 'js' ).


* Create stream factory

lr_streamfactory = lr_ixml->create_stream_factory( ).

* Create output stream

lr_ostream = lr_streamfactory->create_ostream_uri( system_id = file_path ).

* Create renderer

lr_renderer = lr_ixml->create_renderer( ostream = lr_ostream

document = lr_document ).

* Set pretty print

lr_ostream->set_pretty_print( abap_true ).

* Renders the attached document into output stream

lr_renderer->render( ).


The code listing shown above saves the XML file in the application server directory, as shown here.


XML File

Editor’s note: This post has been adapted from a section of the book Complete ABAP by Kiran Bandari


Complete ABAP
Complete ABAP

Get everything you need to code with ABAP, all in one place! Are you a beginner looking for a refresher on the basics? You'll get an overview of SAP architecture and learn syntax. Already an experienced programmer and looking to improve your ABAP skills? Dive right into modifications and code enhancements. Understand the programming environment and build reports, interfaces, and applications with this complete reference to coding with ABAP!

Learn More

SAP PRESS is the world's leading SAP publisher, with books on ABAP, SAP S/4HANA, SAP CX, intelligent technologies, SAP Business Technology Platform, and more!