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 sorting

Former Member
0 Likes
808

I have an XML document and i need to order certains elements .

My XML document is like this

<doc number = '12343401'>John Smith</doc>

<BEHs>

<BEH id = '04'>2000</BEH>

<BEH id = '02'>200</BEH>

<BEH id = '03'>1230</BEH>

<BEH id = '01'>223</BEH>

</BEHs>

</doc>

I need to order BEH elements using id attribute

1 ACCEPTED SOLUTION
Read only

athavanraja
Active Contributor
0 Likes
713

you receive this xml from some other source or your program creates this ?

in general to sort a XML document you need to use XSLT program command xsl:sort ,

in your case you need to create a xslt program and use it with call transformation to sort your xml file.

Regards

Raja

5 REPLIES 5
Read only

athavanraja
Active Contributor
0 Likes
714

you receive this xml from some other source or your program creates this ?

in general to sort a XML document you need to use XSLT program command xsl:sort ,

in your case you need to create a xslt program and use it with call transformation to sort your xml file.

Regards

Raja

Read only

0 Likes
713

In version 4.6c It is not possible to create XSLT

Read only

Former Member
0 Likes
713

It's impossible to sort an XML in version 4.6 but you can sort in ECC 6.0 with XSLT and sort command

Read only

0 Likes
713

Of course XSLT would be technique of choice if it would be available. The really cool hacker solution of course wouuld be building your own XSLT / XPATH implementation in abap for 4.6- but sometimes the quick and dirty solution is the better choice. In the end if the XML is not to structured / complex it's just a bit hack and slay or SPLIT and CONCATENATE ;o)


DATA:
  lv_xml_old TYPE string,
  lv_xml_new TYPE string,

  ln_beg     TYPE i,
  ln_end     TYPE i,
  ln_len     TYPE i,

  lv_buff    TYPE string,
  lt_buff    TYPE TABLE OF string.

CONCATENATE
  '<doc number = ''12343401''>John Smith</doc>'
  '<BEHs>'
  '<BEH id = ''04''>2000</BEH>'
  '<BEH id = ''02''>200</BEH>'
  '<BEH id = ''03''>1230</BEH>'
  '<BEH id = ''01''>223</BEH>'
  '</BEHs>'
  '</doc>'
  INTO
    lv_xml_old.

REPLACE ALL OCCURRENCES OF '''' IN lv_xml_old WITH '"'.

FIND FIRST OCCURRENCE OF '<BEHs>' IN lv_xml_old MATCH OFFSET ln_beg.
FIND FIRST OCCURRENCE OF '</BEHs>' IN lv_xml_old MATCH OFFSET ln_end.

ln_beg = ln_beg + 6.
ln_len = ln_end - ln_beg.

SPLIT lv_xml_old+ln_beg(ln_len) AT '</BEH>' INTO TABLE lt_buff.

SORT lt_buff.

lv_xml_new = lv_xml_old(ln_beg).

LOOP AT lt_buff INTO lv_buff.
  CONCATENATE
    lv_xml_new
    lv_buff
    '</BEH>'
    INTO
      lv_xml_new.
ENDLOOP.

CONCATENATE
  lv_xml_new
  lv_xml_old+ln_end
  INTO
    lv_xml_new.

REPLACE ALL OCCURRENCES OF '"' IN lv_xml_new WITH ''''.

WRITE lv_xml_new.

hf

Roman

Read only

0 Likes
713

I try to use SAP parser no a solution based in strings