Vijay Kumar
Vijay Kumar

Reputation: 21

Extract values from xml in php

I have xml in the below format after getting it from the api call using simplexml_load_string php function

SimpleXMLElement Object ( [@attributes] => Array ( [Code] => 0 [Description] => Success )
[XMLVersion] => 8.9.62 ) 

How i will get the value of code & description to use further.

Upvotes: 1

Views: 248

Answers (1)

John C
John C

Reputation: 8415

Based on your code, it looks like you should be able to access the attributes in a number of ways. Assuming the XML

<UserDetails Code="0" Description="Success">
    <FName>Bob</FName>
    etc...

Starting from here:

$res = simplexml_load_string(curl_exec($ch)); 
$result = get_object_vars($res); 
$res_info = get_object_vars($result['UserDetails']); 

Then either of the below should work:

// Accessing from the UserDetails SimpleXMLElement
echo $result['UserDetails']['Description'];
// Via the subsequent
echo $res_info['@attributes']['Description'];

If this doesn't work, maybe you could post the XML part that this comes from, or your code is dumping the Object you posted.

Upvotes: 1

Related Questions