我有一个PHP脚本,可以将MYSQL数据转换为XML(使用CSS进行样式设置)。我需要在这个页面中添加一些HTML,以便页面显示HTML文本和链接。如何将它们添加到php脚本中,而不会返回错误,如:
此页面包含以下错误:第1列第44行的错误:文档末尾的额外内容以下是第一个错误之前的页面呈现。
如果你能尽可能地描述和清晰,那会有很大帮助。感谢
以下是php脚本:
<?php
header("Content-type: text/xml");
$host = "###";
$user = "###";
$pass = "###";
$database = "###";
$xslt_file = "/xmlstyle.css";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT * FROM users WHERE Username = 'Username4';";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version='"1.0'"?>'n";
//$xml_output .= "<?xml-stylesheet href='"$xslt_file'" type='"text/xsl'" ?
$xml_output .= "<?xml-stylesheet href='"$xslt_file'" type='"text/css'" ?>";
$xml_output .= "<Users>'n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$xml_output .= "'t<Person>'n";
$xml_output .= "'t't<Username>" . $row['username'] . "</Username>'n";
$xml_output .= "'t't<Firstname>" . $row['firstname'] . "</Firstname>'n";
$xml_output .= "'t't<Lastname>" . $row['lastname'] . "</Lastname>'n";
$xml_output .= "'t't<Title>" . $row['Title'] . "</Title>'n";
$xml_output .= "'t't<Description>" . $row['Description'] . "</Description>'n";
$xml_output .= "'t't<Location>" . $row['Location'] . "</Location>'n";
$xml_output .= "'t't<Feeling>" . $row['Feeling'] . "</Feeling>'n";
// Escaping illegal characters
$row['text'] = str_replace("&", "&", $row['text']);
$row['text'] = str_replace("<", "<", $row['text']);
$row['text'] = str_replace(">", ">", $row['text']);
$row['text'] = str_replace("'"", """, $row['text']);
$xml_output .= "'t</Person>'n";
}
$xml_output .= "</Users>";
echo $xml_output;
?>
以下是我想显示的一些HTMI的示例:
<a href="http://www.w3schools.com">Visit W3Schools</a></br><hr noshade size="1" width="90%" align="center">
<a href="http://www.w3schools.com">Visit W3Schools</a></br><hr noshade size="1" width="90%" align="center">
<a href="http://www.w3schools.com">Visit W3Schools</a></br><hr noshade size="1" width="90%" align="center">
<a href="http://www.w3schools.com">Visit W3Schools</a></br>
与其尝试将HTML添加到XML文件中,不如考虑在HTML页面中显示XML文档
将xml放入一个外部文件中,然后在要显示数据的php页面中,您可以使用simplexml_load_file()输出xml内容
<?php
function outputxml(){
$file = '/path/to/your/file.xml';
$xml = simplexml_load_file($file);
$xpath = $xml->xpath('USERS/PERSON');
foreach($xpath as $person){
echo '<p>'.$person->[Username].'</p>';
echo '<p>'.$person->[Firstname].'</p>';
echo '<p>'.$person->[Lastname].'</p>';
echo '<p>'.$person->[Title].'</p>';
echo '<p>'.$person->[Description].'</p>';
echo '<p>'.$person->[Location].'</p>';
echo '<p>'.$person->[Feeling].'</p>';
}
}
outputxml();
?>
<a href="http://www.w3schools.com">Visit W3Schools</a><br /><hr class="noshade_center" />
<a href="http://www.w3schools.com">Visit W3Schools</a><br /><hr class="noshade_center" />
<a href="http://www.w3schools.com">Visit W3Schools</a><br /><hr class="noshade_center" />
<a href="http://www.w3schools.com">Visit W3Schools</a><br />
正如您所知:正斜杠位于break标记和hr标记上的"br"之后,您应该使用css为hr标记设置样式。