php - how to get the values from keys on soap xml -


i have soap xml have challenge getting values without using indexes.

the xml:

<?xml version="1.0" encoding="utf-8"?> <result xmlns="http://api-v1.gen.mm.vodafone.com/mminterface/result">   <resulttype>0</resulttype>   <resultcode>0</resultcode>   <resultdesc>the service request processed successfully.</resultdesc>   <resultparameters>     <resultparameter>       <key>transactionamount</key>       <value>500</value>     </resultparameter>     <resultparameter>       <key>transactionreceipt</key>       <value>lib967vygf</value>     </resultparameter>   </resultparameters>   <referencedata>     <referenceitem>       <key>queuetimeouturl</key>       <value>https://xx.xx.xx.xx:8080/notificationservice?wsdl</value>     </referenceitem>   </referencedata> </result> 

to values, parse xml;

$xml=simplexml_load_string($response) or die("error: cannot create object"); 

then values object;

$resulttype=$xml->resulttype; $resultcode=$xml->resultcode; $resultdesc= $xml->resultdesc; 

the rest on <resultparameters> tags challenge.

i want to them in other safe way apart indexes change. fail if <resultparameter> interchanged.

this them;

$amount= $xml->resultparameters->resultparameter[0]->value; $transactionreceipt= $xml->resultparameters->resultparameter[1]->value;  

anyone share secret here.

using xpath can find <key>transactionamount</key> entry , track value there. being xpath, default namespace sort of gets in way. each element has referenced prefix:elementname.

so first register namespace (giving easier prefix) , retrieve using <key> start point.

$xml=simplexml_load_string($response); $xml->registerxpathnamespace("default", "http://api-v1.gen.mm.vodafone.com/mminterface/result");  $amount= (string)$xml->xpath("//default:key[text()='transactionamount']/../default:value/text()")[0];  echo "amount=".$amount.php_eol; 

as ->xpath returns array - take first item it. still object, cast string makes more useful (in general).

do similar thing of other values need.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -