Skip to Content
Technical Articles
Author's profile photo Kuntal Basu

Create an XML file with Node and Attribute Value

I am working with a legal requirement where an XML has to be uploaded in specific format in Government website. The XML as obvious has different node and their values to be populated. In the XML there are some nodes where attributes also need to be populated. The XML has deep structure. Class CL_XML_DOCUMENT is used for creating the XML. In the example I have used a simple standard table. But in the original requirement we actually has used a Nested Custom structure.

Step 1 – Get the data in your internal table or structure.
Table Format
SELECT
land1,
landk,
lnplz,
prplz,
addrs,
xplzs,
xplpf,
spras,
xland,
xaddr,
nmfmt
FROM t005 INTO table @DATA(lt_t005)
WHERE ( land1 = 'GB' OR land1 = 'IN' )
.

Structure Format

SELECT SINGLE
land1,
landk,
lnplz,
prplz,
addrs,
xplzs,
xplpf,
spras,
xland,
xaddr,
nmfmt
FROM t005 INTO @DATA(ls_t005)
WHERE ( land1 = 'GB' ).

Step 2 – Create the Object and Call the method CREATE_WITH_DATA to create the basic XML

CREATE OBJECT xml_head_doc.
.

For table format
CALL METHOD xml_head_doc->create_with_data
EXPORTING
name = 'Country'
dataobject = lt_t005
RECEIVING
retcode = DATA(lv_retcode).

For structure format
CALL METHOD xml_head_doc->create_with_data
EXPORTING
name = 'Country'
dataobject = ls_t005
RECEIVING
retcode = DATA(lv_retcode).

Step 3 – Call the method to get the node for which attribute to be created

CALL METHOD xml_head_doc->find_node
EXPORTING
name = 'LAND1'
RECEIVING
node = DATA(lo_node).

Step 4 – Populate the attribute passing the node object and the attribute value

CALL METHOD xml_head_doc->set_attribute
EXPORTING
name = 'CName'
value = 'United Kingdom'
node = lo_node
RECEIVING
retcode = lv_retcode.

Step 5 – Add extra node conditionally

      CALL METHOD xml_head_doc->get_node_value
EXPORTING
node  lo_node
RECEIVING
value DATA(lv_data).

if lv_data = ‘GB’

          CALL METHOD xml_head_doc->create_simple_element
EXPORTING
name     ‘Continent’
value    ‘Europe’
parent   lo_node
RECEIVING
new_node DATA(lo_new_node).

endif.

Step 6 – Get the XML in string to write it in the file

CALL METHOD xml_head_doc->render_2_string
IMPORTING
retcode = lv_retcode
stream = DATA(lv_stream)
.

Output
Table Format

<?xml version=”1.0″ encoding=”UTF-16″?>
-<Country>
-<item>
-<LAND1 CName=”United Kingdom”>
GB
<Continent>Europe</Continent>
</LAND1>
<LANDK>GB</LANDK>
<LNPLZ>09</LNPLZ>
<PRPLZ>5</PRPLZ>
<ADDRS>006</ADDRS>
<XPLZS>X</XPLZS>
<SPRAS>E</SPRAS>
<XLAND>X</XLAND>
<XADDR>X</XADDR>
</item>
-<item>
<LAND1>IN</LAND1>
<LANDK>IND</LANDK>
<LNPLZ>06</LNPLZ>
<PRPLZ>4</PRPLZ>
<ADDRS>010</ADDRS>
<SPRAS>E</SPRAS>
<XLAND>X</XLAND>
<XADDR>X</XADDR>
</item>
</Country>

Structure Format
<?xml version=”1.0″ encoding=”UTF-16″?>
-<Country>
-<LAND1 CName=”United Kingdom”>
GB
<Continent>Europe</Continent>
</LAND1>
<LANDK>GB</LANDK>
<LNPLZ>09</LNPLZ>
<PRPLZ>5</PRPLZ>
<ADDRS>006</ADDRS>
<XPLZS>X</XPLZS>
<SPRAS>E</SPRAS>
<XLAND>X</XLAND>
<XADDR>X</XADDR>
</Country>

So following this we are actually able to create an XML file very easily without using the XSLT tool. Adding the attributes to node and conditional additions of node is also possible through this.

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Proposal to beautify your blog post, you have a button to do that:

      CREATE OBJECT xml_head_doc.
      Author's profile photo Enno Wulff
      Enno Wulff

      Thanks for sharing, Kuntal Basu ! Very interesting! 👍

      Author's profile photo Beyhan MEYRALI
      Beyhan MEYRALI

      Hi Kuntal, thanks for sharing!