Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php读取xml文件在@attributes上循环?_Php - Fatal编程技术网

php读取xml文件在@attributes上循环?

php读取xml文件在@attributes上循环?,php,Php,我有一个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" Tra

我有一个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>

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

<?php
$xml = simplexml_load_file( "Service/Forex_Content.xml" );
foreach( $xml->Exrate[0]->attributes() as $a => $b ) {
    echo $a . '="' . $b ."\"\n";
}
试试这个:

function recurseXML($xml, $step)
{
    echo "<table cellpadding=\"2\" cellspacing=\"2\" width=\"100%\" border=\"1\">";
    $step++;
    foreach($xml as $key0 => $value)
    {
        if($key0=='Exrate')
        {           
            echo "\n<tr>\n";                   
            foreach($value->attributes() as $attributeskey0 => $attributesvalue1)
            {    
                echo " <td> [$attributeskey0] = $attributesvalue1</td>\n";  
            }
            echo "</tr>\n\n";
        }
        else
        {
           echo "\n<tr><td colspan=\"5\">$value</td></tr>";
        }           
    }
    echo "</table>\n";
}
$xml = simplexml_load_file("http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx");
recurseXML($xml, 0);

$value->attributes()
替换
$value->@attributes
。您可能需要进一步沿着树向下移动才能到达所需的节点,但您可以对任何项目调用
attributes()

函数recurseXML($xml,$step)
{
回声“;
$step++;
foreach($xml作为$key0=>$value)
{
如果($key0=='Exrate')
{           
回显“\n\n”;
foreach($value->attributes()作为$attributeskey0=>$attributesvalue1)
{    
回显“[$attributeskey0]=$attributesvalue1\n”;
}
回显“\n\n”;
}
其他的
{
回显“\n$value”;
}           
}
回音“\n”;
}
$xml=simplexml\u加载文件(“http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx");
recurseXML($xml,0);

试着解释一下代码。初始代码中有什么错误?
foreach ($value->attributes() as $key=>$val){
    // do something
}
<?php
$xml = simplexml_load_file( "Service/Forex_Content.xml" );
foreach( $xml->Exrate[0]->attributes() as $a => $b ) {
    echo $a . '="' . $b ."\"\n";
}
function recurseXML($xml, $step)
{
    echo "<table cellpadding=\"2\" cellspacing=\"2\" width=\"100%\" border=\"1\">";
    $step++;
    foreach($xml as $key0 => $value)
    {
        if($key0=='Exrate')
        {           
            echo "\n<tr>\n";                   
            foreach($value->attributes() as $attributeskey0 => $attributesvalue1)
            {    
                echo " <td> [$attributeskey0] = $attributesvalue1</td>\n";  
            }
            echo "</tr>\n\n";
        }
        else
        {
           echo "\n<tr><td colspan=\"5\">$value</td></tr>";
        }           
    }
    echo "</table>\n";
}
$xml = simplexml_load_file("http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx");
recurseXML($xml, 0);