[英]XML parsing format using php
我正在嘗試使用PHP解析Xml文件,但是每當我運行代碼mit時,我都會報錯:為foreach()提供的參數無效
XML格式
<?xml version="1.0" standalone="yes"?>
<Rows>
<Row Code="10004" Name="EDEN 46cm TROUGH Terracotta" />
</Rows>
PHP代碼:
$xml = simplexml_load_string(file_get_contents('XML/STKCatigories.xml'));
$i = 0;
foreach($xml->Rows->Row as $key=>$product) {
echo '<li>'.anchor ('/shop/listings/'.$product->Code,$product->Name).'</li>';
}
我不明白我哪里錯了。請幫我
它應該是
$xml = simplexml_load_string(file_get_contents('XML/STKCatigories.xml'));
$prifix = '/shop/listings/' ;
foreach ( $xml as $row ) {
$attr = $row->attributes();
printf('<li>%s</li>', anchor($prifix . $attr->Code, $attr->Name));
}
您正在嘗試訪問標簽屬性,而不是顯式值。 嘗試類似:
$str = <<<XML
<?xml version="1.0" standalone="yes"?>
<Rows>
<Row Code="10004" Name="EDEN 46cm TROUGH Terracotta" />
</Rows>
XML;
$xml = simplexml_load_string($str);
foreach($xml->Row->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
輸出:
SimpleXMLElement Object ( [Row] => SimpleXMLElement Object ( [@attributes] => Array ( [Code] => 10004 [Name] => EDEN 46cm TROUGH Terracotta ) ) ) Code="10004" Name="EDEN 46cm TROUGH Terracotta"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.