在标签中搜索单词(BRL)并输出此标签的全部内容

时间:2019-05-03 14:42:12

标签: php xml search

xml文件中有几个标签。如何在“ BRL”中找到包含其内容的标签?

我尝试过

$usd_brazilRate = $usdXML->item[89]->title;<br>
$usd_brazilDate = $usdXML->item[89]->pubDate;<br>

但项目编号(位置)始终会改变

裁剪的xml内容示例:

<channel>
<item>
        <title>1 USD = 64.78833120 RUB</title>
        <link>http://www.floatrates.com/usd/rub/</link>
        <description>1 U.S. Dollar = 64.78833120 Russian Rouble</description>
        <pubDate>Thu, 2 May 2019 12:00:02 GMT</pubDate>
        <baseCurrency>USD</baseCurrency>
        <baseName>U.S. Dollar</baseName>
        <targetCurrency>RUB</targetCurrency>
        <targetName>Russian Rouble</targetName>
        <exchangeRate>64.78833120</exchangeRate>
        <inverseRate>0.01543488</inverseRate>
        <inverseDescription>1 Russian Rouble = 0.01543488 U.S. Dollar</inverseDescription>
    </item>
    <item>
        <title>1 USD = 3.92245587 BRL</title>
        <link>http://www.floatrates.com/usd/brl/</link>
        <description>1 U.S. Dollar = 3.92245587 Brazilian Real</description>
        <pubDate>Thu, 2 May 2019 12:00:02 GMT</pubDate>
        <baseCurrency>USD</baseCurrency>
        <baseName>U.S. Dollar</baseName>
        <targetCurrency>BRL</targetCurrency>
        <targetName>Brazilian Real</targetName>
        <exchangeRate>3.92245587</exchangeRate>
        <inverseRate>0.25494232</inverseRate>
        <inverseDescription>1 Brazilian Real = 0.25494232 U.S. Dollar</inverseDescription>
    </item>
    <item>
        <title>1 USD = 0.76733706 GIP</title>
        <link>http://www.floatrates.com/usd/gip/</link>
        <description>1 U.S. Dollar = 0.76733706 Gibraltar pound</description>
        <pubDate>Thu, 2 May 2019 12:00:02 GMT</pubDate>
        <baseCurrency>USD</baseCurrency>
        <baseName>U.S. Dollar</baseName>
        <targetCurrency>GIP</targetCurrency>
        <targetName>Gibraltar pound</targetName>
        <exchangeRate>0.76733706</exchangeRate>
        <inverseRate>1.30320826</inverseRate>
        <inverseDescription>1 Gibraltar pound = 1.30320826 U.S. Dollar</inverseDescription>
    </item>
</channel>
$usdXML = simplexml_load_file("http://www.floatrates.com/daily/usd.xml") or die("Failed to load");
$usd_brazilRate = $usdXML->item->title;
$usd_brazilDate = $usdXML->item->pubDate;

输出
1 USD = 3.92245587 BRL
2019年5月2日,星期四,格林尼治标准时间

1 个答案:

答案 0 :(得分:1)

您可以使用XPath:

$brazil = $usdXML->xpath('/channel/item[targetCurrency="BRL"]');
print($brazil[0]->description . "\n");
print($brazil[0]->pubDate . "\n");