<gesmes:Envelope>
is probably not the "whole truth". A prefix like gesmes:
needs to be associated with an uri. In your case it's probably
<gesmes:Envelope
xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01"
xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref"
>
And now you have to deal with namespaces....
There is php's simplexml module. It has some shortcomings when namespaces are involved. But anyway... here it is:
$s = new SimpleXMLelement(getData());
foreach( $s->Cube as $cubeContainer) {
foreach( $cubeContainer as $cubeEntry) {
echo 'time: ', $cubeEntry['time'], "\n";
foreach( $cubeEntry->Cube as $cubeElement) {
echo ' ', $cubeElement['currency'], '=', $cubeElement['rate'], "\n";
}
}
}
// now that was easy ...now the fun of having namespaces/prefixes
foreach( $s->children("http://www.gesmes.org/xml/2002-08-01")->subject as $subject) {
echo 'subject: ', $subject, "\n";
}
foreach( $s->children("http://www.gesmes.org/xml/2002-08-01")->Sender as $sender) {
foreach( $sender->children("http://www.gesmes.org/xml/2002-08-01")->name as $name) {
echo 'sender/name: ', $name, "\n";
}
}
function getData() {
return '<gesmes:Envelope xmlns:gesmes="http://www.gesmes.org/xml/2002-08-01" xmlns="http://www.ecb.int/vocabulary/2002-08-01/eurofxref">
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes:Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes:Sender>
<Cube>
<Cube time="2010-03-26">
<Cube currency="USD" rate="1.3353"/>
<Cube currency="JPY" rate="124.00"/>
<Cube currency="BGN" rate="1.9558"/>
<Cube currency="CZK" rate="25.418"/>
</Cube>
</Cube>
</gesmes:Envelope>';
}
prints
time: 2010-03-26
USD=1.3353
JPY=124.00
BGN=1.9558
CZK=25.418
subject: Reference rates
sender/name: European Central Bank
If you want to use XPath to find specific elements/nodes (either SimpleXMLElement::xpath or DOMXPath which you would use with DOMDocument as pointed out by cletus) you have to register the namespace first.
e.g.
$s = new SimpleXMLelement(getData());
$s->registerXPathNamespace('gesmes', 'http://www.gesmes.org/xml/2002-08-01');
foreach($s->xpath('//gesmes:name[position()=1]') as $n){
echo $n, "\n";
}
prints European Central Bank
.
And there are other functions/methods to parse an XML document. E.g. XMLReader or (which is probably the closest to your your original quest "how can i go about getting the values stored in to a hashmap [...]?") xml_parse_into_struct()