cancel
Showing results for 
Search instead for 
Did you mean: 

ABAP mapping XML inside another XML

Former Member
0 Kudos

<b>Cross posted to ABAP Objects</b>

From XI we want to make an abap mapping.

The input xml looks like this:

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

<ns0:mt_dummy xmlns:ns0="http://kmd.dk/phh/externalEventEngine/pfhaen">

<details>

<Navn>Nielsen</Navn>

<Adresse><![CDATA[><?xml version="1.0" encoding="UTF-8"?><ns0:mt_dummy2 xmlns:ns0="http://kmd.dk/phh/externalEventEngine/pfhaen"><Details2><Vej>tingvej</Vej><Husnr>75</Husnr></Details2></ns0:mt_dummy2>]]></Adresse>

</details>

</ns0:mt_dummy>

One of the fields in this structure <Adresse> contains another xml structure. It is this structure we want as a result of our ABAP mapping. First we get the value of the field <Adresse>. This field contains the actual xml structure that we want to map. We convert the structure to xstring and run it through a new parser and create our output document.(See code below).

The code works fine if we just use a ‘normal’ xml structure, but when one of the fields contains a XML structure and we want to parse this structure, we get the error. Is there anything we have missed, or is this not possible in ABAP mapping ?

If we test the code with SXI_MAPPING_TEST we get no errors, but in runtime we get the following error in SXMB_MONI:

‘The XML page document can not be shown

The XML document must have an element at the top level’

method IF_MAPPING~EXECUTE.

  • initialize xml

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 ).

*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( ).

data: pnode type ref to if_ixml_node,

pnode2 type ref to if_ixml_node,

pnode3 type ref to if_ixml_node.

data: l_blob type string,

l_xml type string,

l_length type i.

pnode = idocument.

pnode2 = pnode->get_first_child( ).

pnode2 = pnode2->get_first_child( ).

pnode2 = pnode2->get_last_child( ).

l_blob = pnode2->get_value( ).

data: l_blob2 type xstring.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'

EXPORTING

TEXT = l_blob

  • MIMETYPE = ' '

  • ENCODING =

IMPORTING

BUFFER = l_blob2

  • EXCEPTIONS

  • FAILED = 1

  • OTHERS = 2

.

IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

*2. parse

*create main factory

data: ixmlfactory2 type ref to if_ixml.

ixmlfactory2 = cl_ixml=>create( ).

*create stream factory

data: streamfactory2 type ref to if_ixml_stream_factory.

streamfactory2 = ixmlfactory2->create_stream_factory( ).

*create input stream

data: istream2 type ref to if_ixml_istream.

istream2 = streamfactory2->create_istream_xstring( l_blob2 ).

*initialize input document

data: idocument2 type ref to if_ixml_document.

idocument2 = ixmlfactory2->create_document( ).

*parse input document

data: iparser2 type ref to if_ixml_parser.

iparser2 = ixmlfactory2->create_parser( stream_factory = streamfactory2

istream = istream2

document = idocument2 ).

iparser2->parse( ).

data: odocument type ref to if_ixml_document.

odocument = idocument2.

data: irc type i.

*render document----


*create output stream

data: ostream type ref to if_ixml_ostream.

ostream = streamfactory2->create_ostream_xstring( result ).

*create renderer

data: renderer type ref to if_ixml_renderer.

renderer = ixmlfactory2->create_renderer( ostream = ostream

document = odocument ).

irc = renderer->render( ).

endmethod.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hey,

It seems like the output is not a valid XML,

(I guess that the input XML is different from your test).

XML must have one element at the top level,

for example:

<A>

Ilan

</A>

<B>

Shani

</B>

is not a valid XML,

A valid XML should look like:

<mt_dummy>

<A>

Ilan

</A>

<B>

Shani

</B>

</mt_dummy>

In order to see the in-valid XML, press on the right-click mouse on the i.e error text,

and chose "view source".

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Problem solved.

The 'error' was in the 'SXI_MAPPING_TEST' transaction. In here the 'CDATA' code was removed automatically and the result was a success, but when we debugged in runtime, the CDATA characters was not removed. When we moved the data with:

replace first occurrence of '<![CDATA[' in l_blob with '' .
replace first occurrence of '' in l_blob with ''.]]>

everything was fine.

Peter