繁体   English   中英

将HTML添加到PHP-> XML脚本

[英]Adding HTML to PHP-->XML script

我有一个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>

而不是尝试将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标签设置样式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM