19

Sorry if this seems like an easy question, but I've started pulling hair out on this...

I have a XML file which looks like this...

<VAR VarNum="90">
  <option>1</option>
</VAR>

I'm trying to get the VarNum.

So far I've been successful using the follow code to get the other information:

$xml=simplexml_load_file($file);
$option=$xml->option;

I just can't get VarNum (the attribute value I think?)

Thanks!

Raj
  • 22,346
  • 14
  • 99
  • 142
Matt
  • 7,022
  • 16
  • 53
  • 66

4 Answers4

25

You should be able to get this using SimpleXMLElement::attributes()

Try this:

$xml=simplexml_load_file($file);
foreach($xml->Var[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

That will show you all the name/value attributes for the first foo element. It's an associative array, so you can do this as well:

$attr = $xml->Var[0]->attributes();
echo $attr['VarNum'];
zombat
  • 92,731
  • 24
  • 156
  • 164
  • 1
    Hi. Thanks for the reply. When I try this I get the following error - "Fatal error: Call to a member function attributes() on a non-object" – Matt Aug 10 '09 at 20:38
  • Thanks! I was able to get this working (it was a syntax error - doh!) Thanks again! – Matt Aug 10 '09 at 20:55
14

What about using $xml['VarNum'] ?

Like this :

$str = <<<XML
<VAR VarNum="90">
  <option>1</option>
</VAR>
XML;

$xml=simplexml_load_string($str);
$option=$xml->option;

var_dump((string)$xml['VarNum']);

(I've used simplexml_load_string because I've pasted your XML into a string, instead of creating a file ; what you are doing with simplexml_load_file is fine, in your case !)

Will get you

string '90' (length=2)

With simpleXML, you access attributes with an array syntax.
And you have to cast to a string to get the value, and not and instance of SimpleXMLElement

For instance, see example #5 of Basic usage in the manual :-)

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 5
    Note: the casting to (string) in the example. – null Aug 10 '09 at 19:46
  • 1
    @null : (nice nichname, by the way ^^) : Thanks! I eddited to add that (and a couple more precisions) – Pascal MARTIN Aug 10 '09 at 19:48
  • 2
    The cast to string is very important when accessing the attributes of a node. We saw some funky behavior (empty value) when we failed to include the cast. – Steven Aug 10 '09 at 19:48
3
[0] => Array
                (
                    [@attributes] => Array
                        (
                            [uri] => https://abcd.com:1234/abc/cst/2/
                        [id] => 2
                    )

                [name] => Array
                    (
                        [first] => abcd
                        [last] => efg
                    )

                [company] => abc SOLUTION
                [email] => abc@xyz.com
                [homepage] => WWW.abcxyz.COM
                [phone_numbers] => Array
                    (
                        [phone_number] => Array
                            (
                                [0] => Array
                                    (
                                        [main] => true
                                        [type] => work
                                        [list_order] => 1
                                        [number] => +919876543210
                                    )

                                [1] => Array
                                    (
                                        [main] => false
                                        [type] => mobile
                                        [list_order] => 2
                                        [number] => +919876543210
                                    )

                            )

                    )

                [photo] => Array
                    (
                        [@attributes] => Array
                            (
                                [uri] => https://abcd.com:1234/abc/cst/2/cust_photo/
                            )

                    )

            )

I applied the below code

$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);

but it is not use full i want all the data in single array in php

Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

Try this

$xml->attributes()['YourPropertyName']; //check property case also
Mahbub Alam
  • 368
  • 2
  • 6