Application Development 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: 

XML - DOM

Former Member
0 Kudos

Hi,

I am using DOM method to parse the XML file. It is working fine. But in some cases I need to get type value also in a node.

For example consider a node....

<nodexx type="zzzzzz"> yyyyy </nodexx>.

In this case I am able to get the node name (nodexx in the above sample) using method get_name( ) and the value (yyyyy in the above sample) using method get_value( ).

How can I get the value of the type mentioned in the xml which is zzzzzz. (I tried checking the method get_type() but in this case it is a custom value)

Thanks,

Krish

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Krishnan Venkataraman ,

check the following thread :

cheers!

gyanaraj

****Pls reward points if u find this helpful

3 REPLIES 3

uwe_schieferstein
Active Contributor
0 Kudos

Hello Krishan

Perhaps the sample report <b>T_PARSING_DOM</b> (package <b>SIXML_TEST</b>) may be helpful for you.

*&---------------------------------------------------------------------*
*& Report  T_DOM_PARSING                                               *
*&                                                                     *
*&---------------------------------------------------------------------*
*& Shows xml DOM parsing using the iXML DOM parser                     *
*&                                                                     *
*&---------------------------------------------------------------------*
REPORT t_dom_parsing  LINE-SIZE 512.
SET EXTENDED CHECK OFF.

*************************************************************
* macros
*
DEFINE xml.
  concatenate xmldata &1 into xmldata.
  write: / 'XML:', &1 color col_positive inverse.
END-OF-DEFINITION.

*************************************************************
* global data
*
DATA: ixml            TYPE REF TO if_ixml,
      streamfactory   TYPE REF TO if_ixml_stream_factory,
      parser          TYPE REF TO if_ixml_parser,
      istream         TYPE REF TO if_ixml_istream,
      document        TYPE REF TO if_ixml_document,
      node            TYPE REF TO if_ixml_node,
      xmldata         TYPE string.


*************************************************************
* xml document
*
START-OF-SELECTION.
  WRITE: /.
  WRITE: / 'XML-Document to parse'.
  WRITE: /.
  xml '<?xml version="1.0"?>'.
  xml '<order number="4711">'.
  xml '   <head>'.
  xml '     <status>confirmed</status>'.
  xml '     <date format="mm/dd/yyyy">08/15/1999</date>'.
  xml '   </head>'.
  xml '   <body>'.
  xml '     <item pos="10" units="2" price="17">abap-book  </item>'.
  xml '     <item pos="20" units="1" price="10">sapr3-cdrom</item>'.
  xml '     <item pos="30" units="5" price="12">coffee     </item>'.
  xml '   </body>'.
  xml '</order>'.

*************************************************************
* xml partsing preparation
*

* create the ixml main factory
  ixml = cl_ixml=>create( ).

* create a stream factory
  streamfactory = ixml->create_stream_factory( ).

* create a input stream
  istream  = streamfactory->create_istream_string( string = xmldata ).

* create a ixml document
  document = ixml->create_document( ).

* create a xml parser
  parser  = ixml->create_parser( document       = document
                                 stream_factory = streamfactory
                                 istream        = istream ).

* parse the xml document into DOM tree
  IF parser->parse( ) <> 0.
    PERFORM process_errors USING parser.
    EXIT.
  ELSE.
    node ?= document.
    PERFORM print_dom USING node.
  ENDIF.



*&---------------------------------------------------------------------*
*&      Form  process_errors
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_PARSER  text
*----------------------------------------------------------------------*
FORM process_errors  USING p_parser TYPE REF TO if_ixml_parser.
  DATA: error  TYPE REF TO if_ixml_parse_error,
        msg    TYPE string,
        count  TYPE i,
        index  TYPE i,
        off    TYPE i.

  WRITE: /.
  WRITE: / 'PARSER-ERRORS'.

  count = p_parser->num_errors( min_severity = if_ixml_parse_error=>co_warning ).
  CHECK count <> 0.
* write error messages
  DO count TIMES.
    index  = sy-index - 1.
    error  = p_parser->get_error( index = index
                                  min_severity = if_ixml_parse_error=>co_warning ).
    msg    = error->get_reason( ).
    off    = error->get_offset( ).
    if error->get_severity( ) = if_ixml_parse_error=>co_warning.
       WRITE: / 'warn :', msg COLOR COL_GROUP    INVERSE, off.
    else.
       WRITE: / 'error:', msg COLOR COL_NEGATIVE INVERSE, off.
    endif.
  ENDDO.
ENDFORM.                    " process_errors
*---------------------------------------------------------------------*
*       FORM print_tree                                               *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM print_dom USING node TYPE REF TO if_ixml_node.
  DATA: iterator  TYPE REF TO if_ixml_node_iterator,
        nodemap   TYPE REF TO if_ixml_named_node_map,
        attr      TYPE REF TO if_ixml_node,
        name      TYPE string,
        prefix    TYPE string,
        value     TYPE string,
        indent    TYPE i,
        count     TYPE i,
        index     TYPE i.

  CHECK NOT node IS INITIAL.

  ULINE.
  WRITE: /.
  WRITE: /' DOM-TREE'.
  WRITE: /.
  IF node IS INITIAL. EXIT. ENDIF.
* create a node iterator
  iterator  = node->create_iterator( ).
* get current node
  node = iterator->get_next( ).

* loop over all nodes
  WHILE NOT node IS INITIAL.
    indent = node->get_height( ) * 2.
    indent = indent + 20.

    CASE node->get_type( ).
      WHEN if_ixml_node=>co_node_element.
*       element node
        name    = node->get_name( ).
        nodemap = node->get_attributes( ).
        WRITE: / 'ELEMENT  :'.
        WRITE: AT indent name COLOR COL_POSITIVE INVERSE.
        IF NOT nodemap IS INITIAL.
*         attributes
          count = nodemap->get_length( ).
          DO count TIMES.
            index  = sy-index - 1.
            attr   = nodemap->get_item( index ).
            name   = attr->get_name( ).
            prefix = attr->get_namespace_prefix( ).
            value  = attr->get_value( ).
            WRITE: / 'ATTRIBUTE:'.
            WRITE: AT indent name  COLOR COL_HEADING INVERSE, '=',
                             value COLOR COL_TOTAL   INVERSE.
          ENDDO.
        ENDIF.
      WHEN if_ixml_node=>co_node_text or
           if_ixml_node=>co_node_cdata_section.
*       text node
        value  = node->get_value( ).
        WRITE: / 'TEXT     :'.
        WRITE: AT indent value COLOR COL_GROUP INVERSE.
    ENDCASE.
*   advance to next node
    node = iterator->get_next( ).
  ENDWHILE.
ENDFORM.                    "traverse_dom

Regards

Uwe

Former Member
0 Kudos

Hi Krishnan Venkataraman ,

check the following thread :

cheers!

gyanaraj

****Pls reward points if u find this helpful

0 Kudos

Thanks guys.. I got the type under attributes.

Thanks,

Krish