2017-06-03 33 views
0

所以試圖採取一個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 
+0

你檢查惠特字符串是否有效? –

+0

我剛剛使用以下來檢查它,它回來了: $ xml = XMLReader :: open($ url); //驗證解析器選項必須啓用 //此方法才能正常工作 $ xml-> setParserProperty(XMLReader :: VALIDATE,true); var_dump($ xml-> isValid()); – benikens

+0

這是整個代碼? –

回答

0

嘗試增加的file_get_contents():如果你想,而不是直接通過網址

$url = 'https://perennialonline.com.au/asset/?req=asset&auth=pere123&do=read_all&fund_id=PVDCIT&start_date=01-01-2013&end_date=01-01-2050' 
$xml = new SimpleXMLElement(file_get_contents($url)); 

,檢查SimpleXMLElement類的簽名:

final public SimpleXMLElement::__construct (
    string $data 
    [, int $options = 0 
    [, bool $data_is_url = false 
    [, string $ns = "" 
    [, bool $is_prefix = false ]]]] 
) 

您必須編寫這樣的:

$xml = new SimpleXMLElement($url, null, true); 
echo $xml->asXml(); 
+0

嘿,擺脫了我的錯誤! – benikens

+0

如果我解決了您的問題,請接受我的答案 – sensorario