我试图从数据库中获取一些格式为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
此页面,则可以正确显示该页面。
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);
?>
有什么想法导致这个问题吗?
答案 0 :(得分:1)
我找到的解决方案是卷曲你的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 Document对象。
simplexml_load_string(getURLContent($url));