Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Traverse all the nodes of an XML document?

I want to traverse through the entire nodes of an if_ixml_document. which is the best way to do this?

Please find the sample document.

<text>
    <id>
         <guid auto="false">
               432543254543
         </guid>
    </id>
     <title>
         <short_title italics="on">
                <bold language = "german">
                     "Hello"
               </bold>
        </short_title>
     </title> </text>

In this document, i need to traverse through the nodes <text>, <id>, <guid> , <title>, <short_title>, <bold> etc.

Thanks in advance

Regards, Alex

like image 550
Alex Avatar asked Oct 04 '11 11:10

Alex


People also ask

How do I get XML nodes?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string.

What is node in XML document?

Everything in an XML document is a node. For example, the entire document is the document node, and every element is an element node. Root node. The topmost node of a tree. In the case of XML documents, it is always the document node, and not the top-most element.

What is traversing the elements and attributes in an XML document called?

A Schema describes the elements and attributes that are valid in an XML document, and the contexts in which they are valid.

What are nodes called in XML?

In the XML DOM, each node is an object. Objects have methods and properties, that can be accessed and manipulated by JavaScript.


3 Answers

The first step is to parse your XML as follows. You can of course upload the XML from a file into the string, but this is just an example:

data: lr_xml type ref to cl_xml_document.
data: lr_node type ref to if_ixml_node.
data: lv_xml type string.

lv_xml = '<text> <id> <guid auto="false"> 432543254543 </guid> </id> <title> <short_title italics="on"> <bold language = "german"> "Hello"</bold> </short_title> </title> </text>'.

create object lr_xml.

lr_xml->parse_string( lv_xml ).
lr_node = lr_xml->get_first_node( ).

Now you have an instance of IF_XML_NODE that points to the root of your XML document. You can now use the various methods to traverse the XML tree, and get values out of it, using the various methods such as GET_CHILDREN, GET_ATTRIBUTES, GET_NAME etc.

This will be OK for fairly small XML documents, though for efficiency, if you are looking for a specific set of nodes, you may want to look at using an XPATH query.

like image 120
mydoghasworms Avatar answered Oct 13 '22 02:10

mydoghasworms


You can find an extensive XML manual on SAP's documentation website (in case the link doesn't work correctly, go to the NetWeaver Developer's Guide on help.sap.com and search for 'xml library').

The chapter 'iXML ABAP Objects Jumpstart' should get you started quickly. The paragraph 'Iterating over the complete DOM-tree' provides the following example code:

data: iterator type ref to if_ixml_node_iterator,
      node     type ref to if_ixml_node.
iterator = document->create_iterator( ).
node = iterator->get_next( ).
while not node is initial.
  * do something with the node
  ...
  node = iterator->get_next( ).
endwhile.
like image 31
René Avatar answered Oct 13 '22 01:10

René


I hope following example can clarify the situation:

 DATA: lcl_xml_doc TYPE REF TO cl_xml_document,
          lf_node TYPE REF TO if_ixml_node,
          lf_value TYPE string,
          i_xml type string,
          lf_name TYPE string,
      i_xml = 'PUT your XML HERE'.
      CREATE OBJECT lcl_xml_doc.
      IF lcl_xml_doc IS BOUND.
        IF lcl_xml_doc->parse_string( i_xml ) EQ 0.
          lf_node = lcl_xml_doc->m_document.
          IF  lf_node IS NOT INITIAL. 
            lf_iterator = lf_node->create_iterator( ).
            lf_node = lf_iterator->get_next( ).
            WHILE NOT lf_node IS INITIAL.
              lf_name = lf_node->get_name( ).
              lf_value = lf_node->get_value( ).
              IF lf_name = 'text'.
               " do something for text
               ENDIF.
              ENDIF.
              lf_node = lf_iterator->get_next( ).
            ENDWHILE.
        ENDIF.

Enjoy, Alexander.

like image 30
Alexander_P Avatar answered Oct 13 '22 03:10

Alexander_P