‎2020 Feb 28 10:29 AM
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>test@gmail.com</Email>
</Content>
Example XML destination:
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<IP>182.235.32.00</IP>
</metadata>
<Content>
<Email>test@gmail.com</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,
‎2020 Feb 28 12:55 PM
With iXML, to go from:
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<Email>test@gmail.com</Email>
</Content>to (new "IP" element):
<?xml version="1.0" encoding="utf-8"?>
<Content>
<IP>182.235.32.00</IP>
<Email>test@gmail.com</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>test@gmail.com</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>test@gmail.com</Email>'
&& '</Content>'.
‎2020 Feb 28 10:49 AM
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>test@gmail.com</Email>
</Content>
‎2020 Feb 28 12:55 PM
With iXML, to go from:
<?xml version="1.0" encoding="UTF-8"?>
<Content>
<Email>test@gmail.com</Email>
</Content>to (new "IP" element):
<?xml version="1.0" encoding="utf-8"?>
<Content>
<IP>182.235.32.00</IP>
<Email>test@gmail.com</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>test@gmail.com</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>test@gmail.com</Email>'
&& '</Content>'.
‎2020 Feb 28 1:26 PM
Sorry for the bad XML 🙂
Thank you very much for your code, very appreciate 🙂