1

I am using simplexml to read all the child nodes successfully. But how do I read the "NumCrds"?

<ACCOUNT NumCrds="1">
<ACCNO>some Bank</ACCNO>
<CURRCODE>CAD</CURRCODE>
<ACCTYPE>00</ACCTYPE>
</ACCOUNT>

I have read it somewhere in the PHP manual but I am unable to find it now.

$my_num_cards=$sxe->ACCOUNT['NumCrds']; 

This is printing the number 1 for all the records even if there are values like 2, 3 in the file.

hakre
  • 193,403
  • 52
  • 435
  • 836
shantanuo
  • 31,689
  • 78
  • 245
  • 403

3 Answers3

3

Attributes can be accessed using array indexes:

$data = '<ACCOUNT NumCrds="1">
<ACCNO>some Bank</ACCNO>
<CURRCODE>CAD</CURRCODE>
<ACCTYPE>00</ACCTYPE>
</ACCOUNT>
';
$xml = new SimpleXMLElement($data);

// this outputs 1
echo $xml['NumCrds'];

It is also possible to use the SimpleXMLElement::attributes() function to returns a list of all of the attribute key/value pairs.

$attributes = $xml->attributes();
echo $attributes['NumCrds'];
Shabbyrobe
  • 12,298
  • 15
  • 60
  • 87
0

Use either $attrs = $el->attributes(); echo $attrs['NumCrds'] or just echo $el['NumCrds']. Attributes are reflected as array elements, while sub-tags are reflected as object properties.

StasM
  • 10,593
  • 6
  • 56
  • 103
0
$my_num_cards=$item->attributes()->NumCrds; 

This is what I was looking for. Thanks for all your help.

http://fr.php.net/manual/en/simplexmlelement.attributes.php#94433

shantanuo
  • 31,689
  • 78
  • 245
  • 403