警告:DOMDocument::load():需要启动标记,在 /Applications/XAMPP/xamppfil


Warning: DOMDocument::load(): Start tag expected, '<' not found in /Applications/XAMPP/xamppfiles/htdocs/university_website/index_xml.php,

我正在尝试从数据库中获取一些格式化为XML的数据。我正在使用DOCDocument和XML样式表从数据库中提取包含数据的XML文件。

我得到的全部错误是:

Warning: DOMDocument::load(): Start tag expected, '<' not found in /Applications/XAMPP/xamppfiles/htdocs/university_website/index_xml.php, line: 10 in /Applications/XAMPP/xamppfiles/htdocs/university_website/xmltrans.php on line 4

index_xml.php包含

<?php
header ("Content-Type:text/xml");
require_once __DIR__ . ('/config/init.php');
$mysqli = new mysqli($db['hostname'], $db['username'], $db['password'], $db['database']);
        if ($mysqli->connect_errno) {
            printf("Connect failed: %s'n", $mysqli->connect_error);
            exit();
        }
$_xml = '<?xml version="1.0"?>';
$_xml = "<news>";
if ($result = $mysqli->query("SELECT * FROM news WHERE live = '1'")) {
    while($news = $result->fetch_array()){ 
        $_xml .= "<news_id>".$news['id']."</news_id>";
        $_xml .= "<title>" .$news['title']."</title>";
        $_xml .= "<content>" .$news['content']."</content>";
        $_xml .= "<live>" .$news['live']."</live>";
    } 
    $result->close();
} 
$_xml .= "</news>";
$xml = simplexml_load_string($_xml);
//$xmlobj = new SimpleXMLElement($_xml);
print $xml->asXML();
?>

如果我导航到此页面本身index_xml.php它会在没有样式的情况下正确显示。

将 xml 文件和 XLST 组合在一起xmltrans.php包含以下代码。如果我导航到此页面,则会出现上面看到的错误。

<?php
$xml = new DOMDocument;
$xml->load('index_xml.php');
$xsl = new DOMDocument;
$xsl -> load('style.xsl');
$proc = new XSLTProcessor;
$proc -> importStyleSheet($xsl);
echo $proc -> transformToXML($xml);
?>

任何想法是什么导致了问题?

我找到的解决方案是卷曲您的 XML 页面。

$url = 'http://localhost/index_xml.php';
function getUrlContent($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; 
Windows NT 5.1; .NET CLR 1.1.4322)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode>=200 && $httpcode<300) ? $data : false;
}

然后使用下面的此函数访问您的 XML 文档对象。

simplexml_load_string(getURLContent($url));

来源:http://php.net/manual/en/book.curl.php

http://php.net/manual/en/function.simplexml-load-file.php