Matt Matt - 3 months ago 9
PHP Question

How to access the data in this PHP XML response?

I'm doing a request to a gov't website for validating a tax number. I get a pretty nested XML response and i'm pretty frustrated at the moment trying to parse and access the data.

In essense this is the response:

<params>
<param>
<value><array><data>
<value><string>UstId_1</string></value>
<value><string>SOMETAXNUM</string></value>
</data></array></value>
</param>
<param>
<value><array><data>
<value><string>ErrorCode</string></value>
<value><string>200</string></value>
</data></array></value>
</param>
<param>
<value><array><data>
<value><string>UstId_2</string></value>
<value><string>SECONDTAXNUM</string></value>
</data></array></value>
</param> ....

</params>


I parse the result with
new SimpleXMLElement()
.

But i have no success accessing what i truely need... And what i need is the "ErrorCode" Response (in this case its 200).

This is what i had

$xml->["param"][1]->["value"]->['array']->["data"]->["value"‌​][1]->['string'];


Thanks to the answer i got, this is my poor solution to get the code i wanted:

$xml = simplexml_load_string($xml_string);
$result = $xml->xpath('param');

$count = 0;
while(list( , $node) = each($result)) {
if ($count == 1){
return (string)$node->value[0]->array[0]->data->value[1]->{'string'};
}
$count ++;
}

Answer
#With something like this

$xml = simplexml_load_string ($xmlstring);
$result = $xml->xpath('param');

while(list( , $node) = each($result)) {
    echo (string)$node->value[0]->array[0]->data->value[0]->{'string'};#UstId_1
}

By the way, very ugly and bad XML example file that you have here. :) Please fromat the xml litte better in you question.