0

我有一个 PHP 脚本,可以将 MYSQL 数据转换为 XML(使用 CSS 设置样式)。我需要在这个页面中添加一些 HTML,以便页面显示 html 文本和链接。我怎样才能将它们添加到 php 脚本中而不会抛出如下错误:

此页面包含以下错误: 第 44 行第 1 列的错误:文档末尾的额外内容 下面是页面的呈现,直到第一个错误。

如果您可以尽可能地描述性和清晰,那将有很大帮助。谢谢

这是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(">", "&gt;", $row['text']); 
        $row['text'] = str_replace("\"", "&quot;", $row['text']); 

    $xml_output .= "\t</Person>\n"; 
} 

$xml_output .= "</Users>"; 

echo $xml_output; 

?> 

这是我想展示的一些 HTMl 的示例:

<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>
4

2 回答 2

1

与其尝试将 HTML 添加到 XML 文件,不如考虑在 HTML 页面中显示 XML 文档

于 2013-07-18T12:42:45.047 回答
0

将您的 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标签设置样式。

于 2016-07-06T21:15:30.040 回答