cancel
Showing results for 
Search instead for 
Did you mean: 

Finding a specific node from the DOM tree

former_member184867
Active Contributor
0 Kudos

Hello Experts,

I need to search a particular type nodes( for example: workspace )  from the following XML.

<app:service xml:base="http://XXX/XXXX/IWFND/RMTSAMPLEFLIGHT/">

<app:workspace>

<atom:title type="text">Data</atom:title>

<app:collection sap:pageable="false" sap:content-version="1" href="BookingCollection"></app:collection>

<app:collection sap:creatable="false" sap:deletable="false" sap:content-version="1" href="CarrierCollection"></app:collection>

<app:collection sap:label="Travel Agencies" sap:searchable="true" sap:content-version="1" href="TravelagencyCollection"></app:collection>

<app:collection sap:searchable="true" sap:content-version="1" href="TravelAgencies"></app:collection>

<app:collection sap:content-version="1" href="SubscriptionCollection"></app:collection>

<app:collection sap:creatable="false" sap:updatable="false" sap:deletable="false" sap:addressable="false" sap:content-version="1" href="NotificationCollection"></app:collection>

<app:collection sap:content-version="1" href="FlightCollection"></app:collection>

</app:workspace>

<atom:link rel="self" href="http://XXX/XXX/RMTSAMPLEFLIGHT/"/><atom:link rel="latest-version" href="http://XXXX/XXXX/RMTSAMPLEFLIGHT/"/>

</app:service>

To get reference of the node I am using following code snippet.

lcl_xml_doc TYPE REF TO cl_xml_document,

CREATE OBJECT lcl_xml_doc .
CALL METHOD lcl_xml_doc->parse_string
   EXPORTING
     stream  = rv_result (rv_result is of type STRING and it holds the above XML)
   RECEIVING
     retcode = v_subrc.

data: node_one type ref to if_ixml_node.
node_one lcl_xml_doc->m_document->find_from_name( name = 'workspace'   ).

However the the above method always returns null and the node_one is always empty ..

Can you tell me where I am wrong ?

Thanks in Advance



Accepted Solutions (1)

Accepted Solutions (1)

iaki_vila
Active Contributor
0 Kudos

Hi Atanu,

I'd rather the Greg option because it is a more direct way to acces the value. I think that your problem could be because you are not using the import parameter namespace, and this can generate problems in order to do the search.

Regards.

former_member184867
Active Contributor
0 Kudos

Hi Inaki and Grzegorz ,

   Thanks for your valuable inputs.

   I am now able to get the node after passing the NAMESPACE parameter. So now the code looks like,

   data: node_one type ref to if_ixml_node.
    node_one lcl_xml_doc->m_document->find_from_name( name = 'workspace' NAMESPACE  = 'app' ).

However I also tried to follow Grzegorz's suggestion and I have used method FIND_NODE. But this time it did not return any node.

   Data: lcl_xml_doc TYPE REF TO cl_xml_document,

   v_node = lcl_xml_doc->find_node( NAME = 'workspace' ) .

  The rest of the code remains as it is.

  

Could you please help me on this.

Best Regards.

former_member184681
Active Contributor
0 Kudos

Hi,

You can try simply with:

v_node = lcl_xml_doc->find_node( NAME = 'app:workspace' ) .

Regards,

Greg

former_member184867
Active Contributor
0 Kudos

Hi Grzegorz Glowacki,

I tried with

v_node = lcl_xml_doc->find_node( NAME = 'app:workspace' ) .

V_NODE is still empty . Any idea why is it empty ?

Regards,

Atanu

iaki_vila
Active Contributor
0 Kudos

Hi Atanu,

Try with this in your original code:

data: node_one type ref to if_ixml_node.

node_one lcl_xml_doc->m_document->find_from_name( name = 'workspace' namespace='app'  ).

Regards.

former_member184867
Active Contributor
0 Kudos

Thanks Inaki for your reply.

As per the scenarios faced here, I find the methods, which does not take NAMESPACE as parameter always fail for my XML. The methods which take the NAMESPACE seem to have fetch the correct node.

For this reason  lcl_xml_doc->m_document->find_from_name fetches the node whereas method lcl_xml_doc->find_node fails as it does not take any NAMESPACE as parameter.

I am facing similar problem while trying to fetch the attributes for the node, where I have to send the NAMESPACE along with the ATTRIBUTE name.

For the same reason the filters were also failing . 

Is this a correct behavior or something is incorrect in my XML?

Best Regards,

Atanu

iaki_vila
Active Contributor
0 Kudos

Hi Atanu,

At first your XML is totally correct, it's neccesary to find the correct method which works properly with namespaces. You should  quit the "Correct Answer" in my answer because you have the problem yet and more people can help you in this thread.

For the atributte you could use the method GET_ATTRIBUTE_NODE_NS in the IF_IXML_ELEMENT interface. In the URI parameter you put the namespace like this case:

    l_attr = l_root->get_Attribute_Node_NS(
                       uri = 'http://schemas.xmlsoap.org/soap/envelope'
                       name = 'mustUnderstand' ).

Regards,

former_member184867
Active Contributor
0 Kudos

Hi Iñaki ,

Thanks for response.

Here my requirement is , I have the reference to a NODE, and I would like to read the value of a particular attribute of that node.  My code looks like

********************************

   data: lr_child      TYPE REF TO if_ixml_node ,

 

   lr_child = lr_iterator_child->get_next( ).

   while not lr_child is INITIAL .     

"  here I need the code to fetch the attribute value for a particular attribute for this node

     lr_child = lr_iterator_child->get_next( ).
   endwhile.

************************************

Here I do not have any reference to the 'ELEMENT', I only have the reference to the node.NODE offers only GET_ATTRIBUTES method which does not take any parameter.

However after using GET_ATTRIBUTES and applying an filtered iterator can be used, but it bocomes a little overburden for this scenario.

Any other approach possible ?

Best Regards,

Atanu

Answers (1)

Answers (1)

former_member184681
Active Contributor
0 Kudos

Hi,

First of all, check if this m_document attribute of your object contains the document. You might also try with different methods of the cl_xml_document class, like FIND_NODE (to get the whole node) or FIND_SIMPLE_ELEMENT (to get the value of the 'workspace' node).

Regards,

Greg