SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and XML error

    Hi guys, ok I'm going through this book called "Learning PHP Data Objects". Just saying so there's a reference. Anyway I'm getting the following error when I upload the code below. The page is saved as books.xm.php. Can anyone help me?

    Thanks

    This page contains the following errors:

    error on line 2 at column 1: Extra content at the end of the document

    Below is a rendering of the page up to the first error.
    HTML Code:
    <?php
    /*
     * this page lists all of the books we have as an XML data structure
     * PDO LibraryManagement example application
     * @author Dennis Popel
    */
    
    // don't forget the include
    include('common.inc.php');
    // set the content type to be XML
    header('Content-Type: application/xml');
    // get the books list
    $books = Model::getBooksWithAuthors();
    
    // echo XML declaration and open rot element
    echo '<?xml version="1.0"?>', "\n";
    echo "<books>\n";
    
    // now iterate over every book and display it
    while($b = $books->fetch()){
    
     ?>
     
     <book id="<?=$b->id?>">
      <isbn><?=$b->isbn?></isbn>
      <title><?=htmlspecialchars($b->title)?></title>
      <publisher><?=htmlspecialchars($b->publisher)?></publisher>
      <summary><?=htmlspecialchars($b->summary)?></summary>
      <author>
       <id><?=$b->author?></id>
       <lastName><?=$b->lastName?></lastName>
       <firstName><?=$b->firstName?></firstName>
      </author>
     </book>
     
     <?php
     
    } // end while($b = $books->fetch()){} loop
    
    echo '</books>';
    
    ?>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,250
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    I'm kind of surprised you're not getting "headers already sent" errors.

    (BTW you showed code, not the output)

    I know it makes the code easier to read with empty lines, but try removing the first couple.

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page outputs

    This page contains the following errors:

    error on line 2 at column 1: Extra content at the end of the document

    Below is a rendering of the page up to the first error.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    447
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Forgive my posting as I'm learning php myself. But in that last loop, you have a lot of constructs that are basically <?=htmlspecialchars($var)?> - is that an alternate method of embedding php within html, or a lot of typos? I'd have done it as

    Code:
    <?php echo htmlspecialchars($var); ?>
    - is that just a longer way of doing the same thing or related to the PDO library being used?
    http://www.firenza.net - my homage to a car from the 1970s

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,250
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)
    Those are "short tags". They work OK as long as PHP is configured with them enabled. I've never used them as having "echo" doesn't add that much more verbosity and the code will still work if short tags are not enabled.

    Maybe looking at view-source would show something helpful?

    And instead of the sequential "echos" maybe string concatenation with a single echo would work?
    eg.
    instead of
    echo "<tag>";
    try
    $xml_string .= "<tag>";
    ......
    echo $xml_string;

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2006
    Location
    Nantwich, Cheshire
    Posts
    447
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    Those are "short tags". They work OK as long as PHP is configured with them enabled. I've never used them as having "echo" doesn't add that much more verbosity and the code will still work if short tags are not enabled.
    Ah, thanks for that. I did a quick search but it confused Google. Back to topic everyone.
    http://www.firenza.net - my homage to a car from the 1970s


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •