在xml中打开特定标签

时间:2014-06-24 06:14:04

标签: php xml

我想打开身体的clildren,我的意思是OriginCurrency。我唯一能做的就是打开第一个标签OriginCurrency。

我有以下代码:

<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>

这是php代码:

?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
>?

如果我尝试使用OrigCurrency [1],它就不起作用了。 并且,如果有一种方法用像OrigCurrency ['USD']这样的val显示它,只是为了知道选择哪一个,因为可能有一百个标签并且不是一个专业的方法来计算一百个标签来显示你想要的那个。我想要帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

工作代码:

<?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;
}

输出:

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变量包含您需要的所有内容。这更专业&#34;得到它的方式,因为它是dinamic。

答案 1 :(得分:1)

如果你想把它指向JPY,你也可以做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