Reputation: 37
I want to open clildren of body, I mean OriginCurrency. The only thing I can, is to open just the first tag OriginCurrency.
I have the following code:
<body>
<OrigCurrency val="Euro">
<Cube date="2014-06-20">
<Rate currency="USD">1.3764</Rate>
<Rate currency="CHF">1.2079</Rate>
<Rate currency="GBP">0.8731</Rate>
</Cube>
</OrigCurrency>
<OrigCurrency val="USD">
<Cube date="2014-06-20">
<currency>JPY</currency>
<rate>70.0554</rate>
</Cube>
</OrigCurrency>
<OrigCurrency val="CHF">
<Cube date="2014-06-20">
<Rate currency="USD">1.1379</Rate>
</Cube>
</OrigCurrency>
<OrigCurrency val="GBP">
<Cube date="2014-06-20">
<Rate currency="CAD">1.5648</Rate>
</Cube>
</OrigCurrency>
</body>
Here is the php code:
?php
$xml = simplexml_load_file('curs.xml')or die("Error: Cannot create object");
$eurtousd = $xml->OrigCurrency->Cube->Rate[0]; //works
$eurtochf = $xml->OrigCurrency->Cube->Rate[1]; //works
$eurtogbp = $xml->OrigCurrency->Cube->Rate[2]; //works
$usdtojpy = $xml->OrigCurrency[1]->Cube->Rate; //doesn't work
>?
If I try with OrigCurrency[1], it doesnt work. And, if is there a way to show it with the val like OrigCurrency['USD'], just to know which one is selected, because there may be like one hundred tags and is not a professional way to count one hundred tags to show the one you want. I want help with that. Thank you!
Upvotes: 1
Views: 54
Reputation:
If you want to point it to JPY, you can also do foreach:
$xml_string = '<body> <OrigCurrency val="Euro"> <Cube date="2014-06-20"> <Rate currency="USD">1.3764</Rate> <Rate currency="CHF">1.2079</Rate> <Rate currency="GBP">0.8731</Rate> </Cube> </OrigCurrency> <OrigCurrency val="USD"> <Cube date="2014-06-20"> <currency>JPY</currency> <rate>70.0554</rate> </Cube> </OrigCurrency> <OrigCurrency val="CHF"> <Cube date="2014-06-20"> <Rate currency="USD">1.1379</Rate> </Cube> </OrigCurrency> <OrigCurrency val="GBP"> <Cube date="2014-06-20"> <Rate currency="CAD">1.5648</Rate> </Cube> </OrigCurrency></body>';
$xml = simplexml_load_string($xml_string); // or load file in your case
foreach($xml->OrigCurrency as $currency) {
$val = $currency->attributes();
if($val['val'] == 'USD') {
$usdtojpy = $currency->Cube->rate;
}
}
echo $usdtojpy; // 70.0554
Upvotes: 1
Reputation: 3236
A working code:
<?php
$xml = '<body>
<OrigCurrency val="Euro">
<Cube date="2014-06-20">
<Rate currency="USD">1.3764</Rate>
<Rate currency="CHF">1.2079</Rate>
<Rate currency="GBP">0.8731</Rate>
</Cube>
</OrigCurrency>
<OrigCurrency val="USD">
<Cube date="2014-06-20">
<currency>JPY</currency>
<rate>70.0554</rate>
</Cube>
</OrigCurrency>
<OrigCurrency val="CHF">
<Cube date="2014-06-20">
<Rate currency="USD">1.1379</Rate>
</Cube>
</OrigCurrency>
<OrigCurrency val="GBP">
<Cube date="2014-06-20">
<Rate currency="CAD">1.5648</Rate>
</Cube>
</OrigCurrency>
</body>';
$dom = new DOMDocument('1.0', 'utf-8');
$dom->xmlStandalone = false;
$dom->loadXML($xml);
$OrigCurrency = $dom->getElementsByTagName('OrigCurrency');
$new_array = array();
foreach ($OrigCurrency as $row) {
// OrigCurrency node
$currency = $row->getAttribute('val');
// Cube node
$cube_node = $row->getElementsByTagName('Cube')->item(0);
$date = $cube_node->getAttribute('date');
// currency node
$currency_node = $cube_node->getElementsByTagName('currency');
$rates = array();
if ($currency_node->length > 0) { // is currency + rate
$rate_node = $cube_node->getElementsByTagName('rate');
$rates[$currency_node->item(0)->nodeValue] = $rate_node->item(0)->nodeValue;
} else { // is Rate and currency attribute
$rate_nodes = $cube_node->getElementsByTagName('Rate');
foreach ($rate_nodes as $row2) {
$rate_currency = $row2->getAttribute('currency');
$rate_value = $row2->nodeValue;
$rates[$rate_currency] = $rate_value;
}
}
$new_array[$currency][$date] = $rates;
}
Outputs:
Array
(
[Euro] => Array
(
[2014-06-20] => Array
(
[USD] => 1.3764
[CHF] => 1.2079
[GBP] => 0.8731
)
)
[USD] => Array
(
[2014-06-20] => Array
(
[JPY] => 70.0554
)
)
[CHF] => Array
(
[2014-06-20] => Array
(
[USD] => 1.1379
)
)
[GBP] => Array
(
[2014-06-20] => Array
(
[CAD] => 1.5648
)
)
)
$new_array
variable contains all you need. This is more "professional" way of getting it, as it is dinamic.
Upvotes: 1