在php中读取xml节点

I am trying to read some specific child nodes from xml in php which i want to display but i m not able to display the node values.Following is my xml format which i m trying to read

<nns2:ApiDirectory xmlns:ns2="https://sourceurl.com/api>
   <api:groups>
        <entry> 
            <key>Affiliate</key>
            <value>
                <apilisting>
                    <entry>
                        <key>value</key>
                            <value>
                                <get>https://url1.com</get>
                            </value>
                    </entry>
                </apilisting>
            </value>
        </entry>
    </api>
</ns2>

I want to read get node from the above xml format.there are about 10-12 fragments like above in xml file.So i want to read get node value from all fragments and put them in an array.

I am using simplexmlelement in php for reading xml Please guide on how to get required node value lets say i put whole of xml above in variable called data,which i have used below.

$xml = new SimpleXMLElement($data);

echo $xml->ns2->api:groups->entry->value->apilisting-entry->value->{get};

There is a typo in your code.

echo $xml->ns2->api:groups->entry->value->apilisting-entry->value->{get};

should be:

echo $xml->ns2->api:groups->entry->value->apilisting->entry->value->{get};

Note the -> after apilisting

And be aware of the :groups namespacing in your xml example.

To make PHP understand you have to give typecasting forcefully on object like below:

$array = (array) $xml->ns2->api:groups->entry->value->apilisting->entry->value->get; 

print_r($array);

OUTPUT :

Array ([0] => 'https://url1.com')