2017-06-03 106 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