Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to replace the XML root tag

Former Member
0 Kudos
875

Hi All,

how to replace the root tag with the certain string in ABAP Mapping

just like

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

<root>

---<header>

-


<name>Lawrence</name>

---</header>

---<Body>

-


<value>test</value>

---<Body>

</root>

into

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

<myRoot>

---<header>

-


<name>Lawrence</name>

---</header>

---<Body>

-


<value>test</value>

---<Body>

</myRoot>

the following is my abap code:

method IF_MAPPING~EXECUTE.

  • initialize iXML

type-pools: ixml.

class cl_ixml definition load.

  • create main factory

data: ixmlfactory type ref to if_ixml.

ixmlfactory = cl_ixml=>create( ).

  • create stream factory

data: streamfactory type ref to if_ixml_stream_factory.

streamfactory = ixmlfactory->create_stream_factory( ).

  • create input stream

data: istream type ref to if_ixml_istream.

istream = streamfactory->create_istream_xstring( source ).

  • parse input document =================================================

  • initialize input document

data: idocument type ref to if_ixml_document.

idocument = ixmlfactory->create_document( ).

  • parse input document

data: iparser type ref to if_ixml_parser.

iparser = ixmlfactory->create_parser( stream_factory = streamfactory

istream = istream

document = idocument ).

iparser->parse( ).

  • get message content of tag root element

data: l_root type ref to if_ixml_element.

l_root = idocument->get_root_element( ).

data: l_children type ref to IF_IXML_NODE_LIST.

l_children = l_root->GET_CHILDREN( ).

    • build up output document =============================================

    • create output document

data: odocument type ref to if_ixml_document.

odocument = ixmlfactory->create_document( ).

  • create the root element

data: root type ref to if_ixml_node.

root = odocument->create_element( name = 'myRoot' ).

*append the children to the new tag

data: l_index type i.

data: l_length type i.

data irc type i.

data: temp_node type ref to IF_IXML_NODE.

l_length = l_children->get_length( ).

while ( l_index < l_length ).

temp_node = l_children->get_item( l_index ).

irc = root->append_child( temp_node ).

l_index = l_index + 1.

endwhile.

odocument->append_child( root ).

data: ostream type ref to if_ixml_ostream.

ostream = streamfactory->create_ostream_xstring( result ).

  • create renderer

data: renderer type ref to if_ixml_renderer.

renderer = ixmlfactory->create_renderer( ostream = ostream

document = odocument ).

irc = renderer->render( ).

endmethod.

but i only got header in my xml without Body element

help me please

thanks in advance

3 REPLIES 3

former_member696081
Participant
0 Kudos
139

Hi Lawrence,

did you got the solution? I am also having the same kind of requirement and to add two attributes to the root element.

<?xml version="1.0" encoding ="UTF-8"?>
<Response IssuerID="1" Version="11.3">
  -Child elemetn
  -Child element2
  -.......
  ........
</Response>

here Response is the root element and the attributes are IssuerID and Version.

Can you please suggest how can we get this.

Regards,

Ranganadh.

0 Kudos
139

Got it.

Just need to create the root element and set the attributes using the set_attribute method.

w_element_inv1  = w_document->create_simple_element(
                  name = 'OrbiscomRequest'
                  parent = w_document ).

      w_element_inv1->set_attribute( name = 'Version'
                                     namespace = ''
                                     value = '11.1' ).

      w_element_inv1->set_attribute( name = 'IssuerID'
                                     namespace = ''
                                     value = '1' ).

Former Member
0 Kudos
139

To do that, the following can be done

lif_ixml_document is the factory

lif_ixml_elementn is the new root

lif_ixml_elemento is the old root

lif_ixml_elemento = lif_ixml_document->get_root_element( ).

lif_ixml_elementn = lif_ixml_document->create_simple_element_ns( name = 'myroot' parent = lif_ixml_document ).

lif_ixml_elementn->if_ixml_node~append_child( lif_ixml_elemento ).