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

XML structure

former_member219850
Participant
0 Likes
10,131

Hi folks,

Which method is used to get below xml structure.

<DeletedECN STATUS="0" TYPE="Unknown" NAME="DeletedECN"> </DeletedECN>

Thanks,

darshan

1 ACCEPTED SOLUTION
Read only

rosenberg_eitan
Active Contributor
0 Likes
9,857

Hi,

Some code based on solution

*----------------------------------------------------------------------*

FORM test_03 .

  DATA: ob_ixml             TYPE REF TO if_ixml . " Interface of Factory Object

  DATA: ob_ixml_document TYPE REF TO if_ixml_document, " XML Document in DOM Representation

        ob_ixml_encoding TYPE REF TO if_ixml_encoding. " Character Encoding

  ob_ixml = cl_ixml=>create( ).

  ob_ixml_document = ob_ixml->create_document( ).

  DATA: ob_ixml_element TYPE REF TO if_ixml_element . " Element of an XML Document

  ob_ixml_element = ob_ixml_document->create_simple_element(

          name = 'DeletedECN'

          parent = ob_ixml_document ) .

  ob_ixml_element->set_attribute( EXPORTING name = 'TYPE' value = 'Unknown' ) .

  ob_ixml_element->set_attribute( EXPORTING name = 'NAME' value = 'DeletedECN' ) .

*  ob_ixml_element->set_value( ' ' ) .

  DATA: ob_ixml_stream_factory TYPE REF TO if_ixml_stream_factory. " Factory for Streams

  DATA: ob_ostream  TYPE REF TO if_ixml_ostream . " Output Streams

  DATA: debug TYPE string.

* For debug purpose only

  ob_ixml_stream_factory = ob_ixml->create_stream_factory( ).

  ob_ostream = ob_ixml_stream_factory->create_ostream_cstring( debug ).

  ob_ostream->set_pretty_print( abap_true ).

  ob_ixml_document->render( ostream = ob_ostream ) .

* At this point we have XML

* Look at debug using the XML view .

* See the content of count vs the XML output using tabular view

  BREAK-POINT .

ENDFORM.

*----------------------------------------------------------------------*

28 REPLIES 28
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
9,857

And, of course,the documentation.

Read only

0 Likes
9,857

Hi Horst,

I am using below code:

lv_string3 = 'DeletedECN STATUS="0" TYPE="Unknown" NAME="DeletedECN"'.

ref3_ixml_element = ref_ixml_document->create_element(

       name      = lv_string3 ).

ref_ixml_node_parent->append_child( new_child = ref3_ixml_element ).


through which  i am getting


<DeletedECN STATUS="0" TYPE="Unknown" NAME="DeletedECN">



Read only

rosenberg_eitan
Active Contributor
0 Likes
9,858

Hi,

Some code based on solution

*----------------------------------------------------------------------*

FORM test_03 .

  DATA: ob_ixml             TYPE REF TO if_ixml . " Interface of Factory Object

  DATA: ob_ixml_document TYPE REF TO if_ixml_document, " XML Document in DOM Representation

        ob_ixml_encoding TYPE REF TO if_ixml_encoding. " Character Encoding

  ob_ixml = cl_ixml=>create( ).

  ob_ixml_document = ob_ixml->create_document( ).

  DATA: ob_ixml_element TYPE REF TO if_ixml_element . " Element of an XML Document

  ob_ixml_element = ob_ixml_document->create_simple_element(

          name = 'DeletedECN'

          parent = ob_ixml_document ) .

  ob_ixml_element->set_attribute( EXPORTING name = 'TYPE' value = 'Unknown' ) .

  ob_ixml_element->set_attribute( EXPORTING name = 'NAME' value = 'DeletedECN' ) .

*  ob_ixml_element->set_value( ' ' ) .

  DATA: ob_ixml_stream_factory TYPE REF TO if_ixml_stream_factory. " Factory for Streams

  DATA: ob_ostream  TYPE REF TO if_ixml_ostream . " Output Streams

  DATA: debug TYPE string.

* For debug purpose only

  ob_ixml_stream_factory = ob_ixml->create_stream_factory( ).

  ob_ostream = ob_ixml_stream_factory->create_ostream_cstring( debug ).

  ob_ostream->set_pretty_print( abap_true ).

  ob_ixml_document->render( ostream = ob_ostream ) .

* At this point we have XML

* Look at debug using the XML view .

* See the content of count vs the XML output using tabular view

  BREAK-POINT .

ENDFORM.

*----------------------------------------------------------------------*

Read only

0 Likes
9,857

Hi Eitan,

thank you for your reply.

i understood your solution.

but i am unable to get last closing node i.e. </DeletedECN>

<DeletedECN STATUS="0" TYPE="Unknown" NAME="DeletedECN"> </DeletedECN>..


how can i achieve this.


Thanks,

Darshan.

Read only

0 Likes
9,857

Hi Darshan,

I have faced this issue before. What I observed is, SAP uses a "self-closing tag" in case there are no elements under the node. I haven't found out a setting on SAP to make it generate a closing tag.

Ref: XML Elements

Thanks, Juwin

Read only

0 Likes
9,857

Look at the picture from Eitan. The Tag is closed at the end, it does not need to have separate closing...

Read only

0 Likes
9,857

Hi

Yes, syntactically, XML is correct. But, in my case, Bank wasn't accepting that kind of tag. They wanted an explicit closing tag and not a 'self-closing' tag. And, that's what I think OP wants to achieve as well.

Thanks,

Juwin

Read only

0 Likes
9,857

Hi,

This is a valid XML .

Since you do no use any value there is no closing tag .

If you use it:

You get:

There might be an option to force the ob_ostream to produce a closing tag but I do not see it.

Regards.

Read only

0 Likes
9,857

Hi ,

That's a nice trick. I would just suggest to replace the value *, with a new line character, so that the receiver doesn't know that the closing tag was forced. Plus adding a new line character, it doesn't visually add a new value to the file at all.

Thanks,

Juwin

Read only

0 Likes
9,857

Hi Eitan,

I appreciate your help,,

How can i get status attribute?.

Thanks,

Darshan

Read only

0 Likes
9,857

Good one

Read only

0 Likes
9,857

Add one more attribute just like he has given in the sample code:

ob_ixml_element->set_attribute( exporting name = 'STATUS' value = '0' )


Thanks,

Juwin

Read only

0 Likes
9,857

Hi,

I really think that you have to use    advise.

ob_ixml_document->get_elements_by_tag_name( name = 'Tag name' ).

Regards.

Read only

0 Likes
9,857

Hi Eitan,

I cant get this.

closing not coming.

Thanks,

Darshan

Read only

0 Likes
9,857

Or better add some unique dummy value like "[SAP_DUMMY]".

And clear it from XLM string at the end.

-- Tomas --
Read only

0 Likes
9,857

Hi Darshan,

It does appear. Complete code is:

    1  report xmlexample.

    2 

    3  data:ob_ixml                type ref to if_ixml .                              " Interface of Factory Object

    4  data:ob_ixml_document type ref to if_ixml_document,                      " XML Document in DOM Representation

    5       ob_ixml_encoding type ref to if_ixml_encoding.                      " Character Encoding

    6 

    7  ob_ixml = cl_ixml=>create( ).

    8 

    9  ob_ixml_document = ob_ixml->create_document( ).

   10 

   11  data:ob_ixml_element        type ref to if_ixml_element .                      " Element of an XML Document

   12 

   13  ob_ixml_element = ob_ixml_document->create_simple_element(

   14          name = 'DeletedECN'

   15          parent = ob_ixml_document ) .

   16 

   17  ob_ixml_element->set_attribute( exporting name = 'STATUS' value = '0' ) .

   18 

   19  ob_ixml_element->set_attribute( exporting name = 'TYPE' value = 'Unknown' ) .

   20 

   21  ob_ixml_element->set_attribute( exporting name = 'NAME' value = 'DeletedECN' ) .

   22  data:  value                  type string value cl_abap_char_utilities=>newline.

   23  ob_ixml_element->set_value( value ) .

   24 

   25  data:ob_ixml_stream_factory type ref to if_ixml_stream_factory.                " Factory for Streams

   26  data:ob_ostream             type ref to if_ixml_ostream .                      " Output Streams

   27 

   28  data:debug                  type string.

   29 

   30  * For debug purpose only

   31  ob_ixml_stream_factory = ob_ixml->create_stream_factory( ).

   32  ob_ostream = ob_ixml_stream_factory->create_ostream_cstring( debug ).

   33  ob_ostream->set_pretty_print( abap_true ).

   34  ob_ixml_document->render( ostream = ob_ostream ) .

   35 

   36  * At this point we have XML

   37  * Look at debug using the XML view .

   38  * See the content of count vs the XML output using tabular view

   39  write debug.

Thanks,

Juwin

Read only

0 Likes
9,857

With the newline character, there is no need to do a replace/ clear at the end. So, isn't that actually better?

Thanks, Juwin

Read only

0 Likes
9,857

Hi juwin,

I am using show_xml for display..

ref12_ixml_element = ref_ixml_document->create_simple_element(

                                                 name = 'DeletedAlternateLinks '

                                                 parent = ref_ixml_document ).

ref12_ixml_element->set_attribute( EXPORTING name = 'STATUS' value = '0' ) .

ref12_ixml_element->set_attribute( EXPORTING name = 'TYPE' value = 'Unknown' ) .

ref12_ixml_element->set_attribute( EXPORTING name = 'Name' value = 'DeletedAlternateLinks ' ) .

ref12_ixml_element->set_value( c_newline ).

ref_ixml_node_parent->append_child( new_child = ref12_ixml_element ).




Thanks,

Darshan

Read only

0 Likes
9,857

This could be a display issue with the viewer. Open the downloaded XML file, using a notepad and then check for the closing tags in notepad.

Thanks, Juwin

Read only

0 Likes
9,857

It is unnecessary character in the output XML.

Might cause problems with some XML parsers (even though it should not).

For example IE shows it parsed like this:

<to> </to>     (with space between)

Chrome:

<to></to>     (without space)

and other IE (older):

<to />

And it will obviously create new line if displayed in simple text editors.

-- Tomas --
Read only

0 Likes
9,857

It is just because of parser.

Raw data (XML string) contains closing tag. You can see it even in your screenshot (debugger)

-- Tomas --
Read only

0 Likes
9,857

ok juwin..

i am using below code for display...

CALL FUNCTION 'SDIXML_DOM_TO_XML'

   EXPORTING

     document      = ref_ixml_document

   IMPORTING

     xml_as_string = lv_xstring

   EXCEPTIONS

     no_document   = 1

     OTHERS        = 2.

IF sy-subrc <> 0.

**--Exception Handling

ENDIF.

**--convert xstring to string

CALL METHOD cl_abap_conv_in_ce=>create

   EXPORTING

     encoding    = 'UTF-8'

     endian      = 'L'

     ignore_cerr = 'X'

     replacement = '#'

     input       = lv_xstring              " Xstring

   RECEIVING

     conv        = convin.

CALL METHOD convin->read

   IMPORTING

     data = lv_xml.

cl_abap_browser=>show_xml(

   EXPORTING

     xml_string   lv_xml ).


what modification i need to do to download xml..


please help.


Thanks,

Darshan

Read only

0 Likes
9,857

You should be able to convert the XML string to a character table using CONVERT_STRING_TO_TABLE and then just use GUI_DOWNLOAD, to get the file out.

Thanks, Juwin

Read only

0 Likes
9,857

Hi tomas,

How can I avoid it??

thanks,

Darshan

Read only

0 Likes
9,857

Avoid what exactly?

-- Tomas --
Read only

0 Likes
9,856

Hi,

A very good idea thanks.

Read only

0 Likes
9,856

Hi Eitan,

How to bring STATUS attribute at beginning...

Thanks,

Darshan

Read only

0 Likes
9,856

Hi Eitan ,

My status = '0' is not at the beginning irrespective of its position..

Thanks,

Darshan