gpt4 book ai didi

php读取xml文件循环遍历@attributes?

转载 作者:可可西里 更新时间:2023-11-01 00:11:01 25 4
gpt4 key购买 nike

我有一个 exrate.xml 看起来像这样

 <!--For reference only. Only one request every 5 minutes!-->
<ExrateList>
<DateTime>5/29/2011 8:54:12 PM</DateTime>
<Exrate CurrencyCode="AUD" CurrencyName="AUST.DOLLAR" Buy="21688.77" Transfer="21819.69" Sell="22201.6"/>
<Source>source name </Source>
</ExrateList>

任何人都知道如何读取 xml 并输出数据。

货币 |购买 |销售

我用过

            <?php
= simplexml_load_file("Service/Forex_Content.xml");
echo '<pre>';
print_r($xml);
echo '</pre>';

?>


SimpleXMLElement Object
(
[DateTime] => 5/29/2011 8:54:12 PM
[Exrate] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[CurrencyCode] => AUD
[CurrencyName] => AUST.DOLLAR
[Buy] => 21688.77
[Transfer] => 21819.69
[Sell] => 22201.6
)

)

如何循环@attributes 来显示数据?

foreach ($xml as $value){
foreach ($value->@attributes as $key=>$val){ // I have problem here @attributes

}

}

最佳答案

使用 SimpleXML,使用 attributes() 方法访问属性:

foreach ($value->attributes() as $key=>$val){
// do something
}

关于php读取xml文件循环遍历@attributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6168465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com