0

I have a PHP script that converts MYSQL data into XML (which is styled using CSS). I need to add a bit of HTML to this page, so that the page displays html text and links. How can I add them to the php script without it throwing back errors like:

This page contains the following errors: error on line 44 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error.

If you could be as descriptive and clear as possible, that would help so much. Thanks

Here is the php script:

<?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; 

?> 

Here's and example of some of the HTMl that I would like to display:

<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>
8
  • 4
    Don't build XML by mashing together strings, use an XML library. Commented Jul 12, 2013 at 15:52
  • If you could post the exact text of the error message that would be helpful. My first comment would be that your HTML is not valid XML. Commented Jul 12, 2013 at 15:53
  • 1
    @googleyberry: That's not how Stackoverflow works. Tell us which part of the error message you don't understand; Also outline which of the many solutions about that exact error message that has been given on this website you have looked into and why - in specific - it didn't help you. Provide reference for those and show with examples what you did to make use of those suggestions. Commented Jul 12, 2013 at 15:59
  • The error is:This page contains the following errors: error on line 44 at column 1: Extra content at the end of the document Below is a rendering of the page up to the first error. Commented Jul 12, 2013 at 16:00
  • Do you understand what "Extra content at the end of the document" means? How would you say it in your own words? Commented Jul 12, 2013 at 16:00

2 Answers 2

1

Instead of trying to add HTML to an XML file, why don't you look at displaying the XML document in an HTML page

Sign up to request clarification or add additional context in comments.

Comments

0

Put your xml into an external file and then in the php page you would like to display the data on you can output the xml contents with simplexml_load_file()

<?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 />

Just so you know: the forward slash goes after the "br" on the break tag and the hr tag, and you should style you hr tags with css.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.