First Rate is not an attribute but an element. So you would need another getElementsByTagName('Rate') and loop over it. However the XML uses a default namespace so getElementByTagNameNS('http://www.bnr.ro/xsd', 'Rate') would be the correct way.
An easier way is to use Xpath to fetch the value directly:
$document = new DOMDocument();
$document->load('http://www.bnr.ro/nbrfxrates.xml');
$xpath = new DOMXpath($document);
$xpath->registerNamespace('r', 'http://www.bnr.ro/xsd');
var_dump(
$xpath->evaluate('number(//r:Cube/r:Rate[@currency="EUR"])')
);
Output:
float(4.4961)
Xpath does not have a default namespace, so you have to register your own alias for it (I used r in the example.).
The Xpath expression
- fetch any
{http://www.bnr.ro/nbrfxrates.xml}Cube
//r:Cube
- fetch all
{http://www.bnr.ro/nbrfxrates.xml}Rate children
//r:Cube/r:Rate
- filter by the
currency attribute
//r:Cube/r:Rate[@currency="EUR"]
- cast the first found node into a number
number(//r:Cube/r:Rate[@currency="EUR"])
Cubetag hasn'tRateattribute. Do you meandateattribute orRatechild?