所以試圖採取一個URL提供的XML,然後格式化數據到一個HTML表格與PHP。我的意思是將某種格式應用於xml,以便它可以將它作爲簡單元素來讀取?解析XML在PHP中,解析器錯誤開始標記預計
$url = 'myxmlurl'
$xml = new SimpleXMLElement($url);
,然後我得到這些漂亮的錯誤:
Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 7
Warning: simplexml_load_string(): in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 7
Warning: simplexml_load_string():^in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 7
編輯:固定的file_get_contents錯誤。現在,當我嘗試循環時,我正在獲得一個新的。
$xml = new SimpleXMLElement(file_get_contents($url));
foreach ($xml->assets->asset as $assetElement) {
$html="
<tr>
<td class='text-left'><strong>"/*.$assetElement.*/."</strong></td>
<td class='text-left'><strong>".(string)$assetElement->effectiveDate."</strong></td>
<td class='text-left'><strong>".(string)$assetElement->buyPrice."</strong></td>
<td class='text-left'><strong>".(string)$assetElement->sellPrice."</strong></td>
</tr>";
}
它不喜歡我的foreach
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wordpress\wp-content\plugins\xmldisplay\test.php on line 26
你檢查惠特字符串是否有效? –
我剛剛使用以下來檢查它,它回來了: $ xml = XMLReader :: open($ url); //驗證解析器選項必須啓用 //此方法才能正常工作 $ xml-> setParserProperty(XMLReader :: VALIDATE,true); var_dump($ xml-> isValid()); – benikens
這是整個代碼? –