在php中解析XML,解析器错误开始标记预期

时间:2017-06-03 03:47:40

标签: php xml

因此尝试使用提供了url的xml,然后使用php将数据格式化为html表。我是否打算对xml应用某种格式,以便它可以将其作为simplexmlelement读取?

$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

1 个答案:

答案 0 :(得分:1)

尝试添加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();