Application Development and Automation 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: 
Read only

Add node into XML stream

0 Likes
2,181

Hi all,

i need to add to an existing xml stream a node on the top of the xml.

Example XML source:

<?xml version="1.0" encoding="UTF-8"?>
<Content>
	<Email>[email protected]</Email>
</Content>

Example XML destination:

<?xml version="1.0" encoding="UTF-8"?>
<metadata>
	<IP>182.235.32.00</IP>
</metadata>
<Content>
	<Email>[email protected]</Email>
</Content>

I think we should use the class if_ixml but sincerely i don't know how to write my need.

I hope you can help me 🙂

Regards,

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,683

With iXML, to go from:

<?xml version="1.0" encoding="UTF-8"?>
<Content>
  <Email>[email protected]</Email>
</Content>

to (new "IP" element):

<?xml version="1.0" encoding="utf-8"?>
<Content>
  <IP>182.235.32.00</IP>
  <Email>[email protected]</Email>
</Content>

this code can be used:

DATA(lo_ixml) = cl_ixml=>create( ).
DATA(lo_streamfactory) = lo_ixml->create_stream_factory( ).
DATA(lo_document) = lo_ixml->create_document( ).

DATA(lo_parser) = lo_ixml->create_parser(
        stream_factory = lo_streamfactory
        istream        = lo_streamfactory->create_istream_string(
                             '<?xml version="1.0" encoding="UTF-8"?>'
                          && '<Content>'
                          && '  <Email>[email protected]</Email>'
                          && '</Content>' )
        document       = lo_document ).
lo_parser->parse( ).

DATA(lo_element) = lo_document->create_element( name = 'IP' ).
lo_element->set_value( '182.235.32.00' ).
lo_document->find_from_name( name = 'Content')->insert_child(
        new_child = lo_element
        ref_child = lo_document->find_from_name( name = 'Email') ).

DATA(l_xstring) = VALUE xstring( ).
DATA(lo_ostream) = lo_streamfactory->create_ostream_xstring( l_xstring ).
DATA(lo_renderer) = lo_ixml->create_renderer(
        ostream  = lo_ostream
        document = lo_document ).
lo_renderer->render( ).

DATA(string) = cl_abap_codepage=>convert_from( l_xstring ).
ASSERT string =
        '<?xml version="1.0" encoding="utf-8"?>'
     && '<Content>'
     && '<IP>182.235.32.00</IP>'
     && '<Email>[email protected]</Email>'
     && '</Content>'.
3 REPLIES 3
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,683

Your "XML destination" is not valid, there can be only a unique root element. Valid XML example:

<?xml version="1.0" encoding="UTF-8"?>
<Content>
<IP>182.235.32.00</IP> <Email>[email protected]</Email> </Content>
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,684

With iXML, to go from:

<?xml version="1.0" encoding="UTF-8"?>
<Content>
  <Email>[email protected]</Email>
</Content>

to (new "IP" element):

<?xml version="1.0" encoding="utf-8"?>
<Content>
  <IP>182.235.32.00</IP>
  <Email>[email protected]</Email>
</Content>

this code can be used:

DATA(lo_ixml) = cl_ixml=>create( ).
DATA(lo_streamfactory) = lo_ixml->create_stream_factory( ).
DATA(lo_document) = lo_ixml->create_document( ).

DATA(lo_parser) = lo_ixml->create_parser(
        stream_factory = lo_streamfactory
        istream        = lo_streamfactory->create_istream_string(
                             '<?xml version="1.0" encoding="UTF-8"?>'
                          && '<Content>'
                          && '  <Email>[email protected]</Email>'
                          && '</Content>' )
        document       = lo_document ).
lo_parser->parse( ).

DATA(lo_element) = lo_document->create_element( name = 'IP' ).
lo_element->set_value( '182.235.32.00' ).
lo_document->find_from_name( name = 'Content')->insert_child(
        new_child = lo_element
        ref_child = lo_document->find_from_name( name = 'Email') ).

DATA(l_xstring) = VALUE xstring( ).
DATA(lo_ostream) = lo_streamfactory->create_ostream_xstring( l_xstring ).
DATA(lo_renderer) = lo_ixml->create_renderer(
        ostream  = lo_ostream
        document = lo_document ).
lo_renderer->render( ).

DATA(string) = cl_abap_codepage=>convert_from( l_xstring ).
ASSERT string =
        '<?xml version="1.0" encoding="utf-8"?>'
     && '<Content>'
     && '<IP>182.235.32.00</IP>'
     && '<Email>[email protected]</Email>'
     && '</Content>'.
Read only

0 Likes
1,683

Sorry for the bad XML 🙂

Thank you very much for your code, very appreciate 🙂