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

Problem with CL_XML_DOCUMENT_BASE->FIND_NODE method

ipravir
Active Contributor
2,887

Hello Experts,

I there any sample program, where system is trying to fetch node value using FIND_NODE method of class CL_XML_DOCUMENT_BASE. Where importing parameter "NAME" is containing path.

for example:

constant lv_name type string value 'node1/node2/node3/node4'.

lo_xml->find_node( EXPORTING name = lv_name RECEIVING node = DATA(lo_node) ).

And there could be a scenario, where node can contains namespace, as below example,

constant lv_name type string value 'nd:node1/node2/xl:node3/node4'.

lo_xml->find_node( EXPORTING name = lv_name RECEIVING node = DATA(lo_node) ).

Regards,

Praveer.

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
1,834

The method FIND_NODE of CL_XML_DOCUMENT_BASE is not compatible with namespaces.

What you can do is to use the public attribute M_DOCUMENT which is an instance of the IF_IXML_DOCUMENT interface, which provides the method FIND_FROM_PATH_NS.

I recommend to use the path with a leading / to start at the root node.

The following code should work for "familyns.xml":

DATA(element) = lo_xml->m_document->find_from_path_ns( 
            default_uri = ''
            path        = '/"fm:family"/"fr:father"/son' ).

(pay attention to the quotes around the names with a namespace)

Note that, in "fm:family" of the path parameter, "fm" is not the namespace prefix but the namespace URI, i.e. "fm" in "fm:family" corresponds to the right-hand side of xlmns:fm="fm". Generally, this value is something like "http://..." so we would have something like this:

            path        = '/"http://...fm...:family"/"http://...fr...:father"/son'.
4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
1,834

Could you format your two portions of code please? (CODE button) Thanks.

Moreover, is "node1" a child node of the root node of your XML document?

Read only

Sandra_Rossi
Active Contributor
0 Likes
1,834

Thanks. Note that it's best to edit your question, as "answers" are to be used when you are proposing a solution to the poster's problem (cf SAP explanation below). PS: you may use the native ABAP type XSTRING, avoid edoc_file that nobody knows.

Read only

Sandra_Rossi
Active Contributor
1,835

The method FIND_NODE of CL_XML_DOCUMENT_BASE is not compatible with namespaces.

What you can do is to use the public attribute M_DOCUMENT which is an instance of the IF_IXML_DOCUMENT interface, which provides the method FIND_FROM_PATH_NS.

I recommend to use the path with a leading / to start at the root node.

The following code should work for "familyns.xml":

DATA(element) = lo_xml->m_document->find_from_path_ns( 
            default_uri = ''
            path        = '/"fm:family"/"fr:father"/son' ).

(pay attention to the quotes around the names with a namespace)

Note that, in "fm:family" of the path parameter, "fm" is not the namespace prefix but the namespace URI, i.e. "fm" in "fm:family" corresponds to the right-hand side of xlmns:fm="fm". Generally, this value is something like "http://..." so we would have something like this:

            path        = '/"http://...fm...:family"/"http://...fr...:father"/son'.
Read only

ipravir
Active Contributor
1,834

Hi,

Please use below XML format and code.

constant: lc_name type string value 'family/father/son/', OR
          lc_name type string value 
	  l_path TYPE pathextern VALUE ''.
DATA: ex_binary       TYPE XSTRING.
OPEN DATASET l_path FOR INPUT IN BINARY MODE  MESSAGE l_mess .
IF sy-subrc EQ 0.
    READ DATASET l_path INTO ex_binary .
    CLOSE DATASET l_path .
    DATA(lo_xml) = NEW cl_xml_document( ).
    lo_xml->parse_xstring( stream = ex_binary ).
    lo_xml->find_node( EXPORTING name = lc_name 
                       RECEIVING node = DATA(lo_node) ).
    IF lo_node IS BOUND.
      DATA(lv_value) = lo_node->get_value( ).
    ENDIF.
ENDIF.

Please node: if LC_NAME equal to 'son', then logic is working fine and lv_value is returning correct value but my requirement, LC_NAME would be node path as declared.

Find below XML file format with and without namespace.

family.xml:

<family>

<father>

<son>Mr. X</son> </father>

</family>

familyns.xml:

<fm:family xmlns:fm="fm">

<fr:father xmlns:fr="fr"> <son>Mr. X</son> </fr:father>

</fm:family>

Regards,

Praveer