SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    New problem with array loop and xml output

    ok so i have a new problem with my script to output my data into xml format. i keep getting an error:

    XML Parsing Error: junk after document element
    Location: http://domain.com/file.php
    Line Number 9, Column 570:

    this line is where the connection is made - the minute i take and place the "<asset>" and "</asset>" outside the loop its fine...however, i need that in the loop.

    the source code come up looking fine


    Here is the code
    *******************
    [ code]// output basic header info and beginning of xml tags
    $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    $result_story = mysql_query( "Select story.storyid, story.created, story.startdate, story.headline, story.subhead, story.byline, story.body, story.comments, story.isactive, story.keepalive, story.fromprint, story.siteid, topic.topicname FROM story, storytopic, topic WHERE story.isactive = 'Y' AND story.storyid=storytopic.storyid AND storytopic.topicid = topic.topicid", $dbhandle );

    while( $row = mysql_fetch_assoc($result_story) ) {
    $output .= "<Asset>";
    $output .= "<AssetType>Story</AssetType>";
    foreach($row as $name=>$value){
    $output .= "<Field>";
    $output .= "<Name>" .htmlentities($name);
    $output .= "</Name>";
    $output .= "<Values>";
    $output .= "<Value>" .htmlentities($value)."</Value>";
    $output .= "</Values>";
    $output .= "</Field>";

    }
    $output .= "</Asset>";
    }

    // tell the browser what kind of file is come in
    header("Content-type: text/xml");
    // print out XML that describes the schema
    echo $output;

    // close the connection
    mysql_close($dbhandle);

    [/code ]
    Last edited by nmpku; Jul 17, 2007 at 06:52.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Do post the output, please. Or post a link to it.
    Saul

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    That looks like malformed xml if you have more than one <Asset>

    Should this line be before the commencement of the loop?

    PHP Code:
    $output .= "<Asset>"
    Or this line inside the loop?

    PHP Code:
    $output .= "</Asset>"

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Actually, I think the problem is a missing document root element. Like:

    Code php:
    $output.="<Assets>";
     
    while( $row = mysql_fetch_assoc($result_story) ) {
      $output .= "<Asset>";
      $output .= "<AssetType>Story</AssetType>";
      foreach($row as $name=>$value){
        $output .= "<Field>";
        $output .= "<Name>" .htmlentities($name);
        $output .= "</Name>";
        $output .= "<Values>";
        $output .= "<Value>" .htmlentities($value)."</Value>";
        $output .= "</Values>";
        $output .= "</Field>";
      }
      $output .= "</Asset>";
    }
     
    $output.="</Assets>";
    Saul

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    yes, my bad, I didn't spot it was a loop in a loop ... I have never come across that error message before though.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That error is not a doing of php, but FF (IE would give 'Only one top level element is allowed in an XML document.') that does the parsing of the XML document.
    Saul

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Right, I follow you now, I thought that message came out of SimpleXML or XSLT or something ...

    Thanks php_daemon

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks daemon..i will try that. Boy have i learned a lot from this project!

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    man...this is driving me to drink. Now the company i am doing this for wants each "story" or inner loop, to write to a file. So basically we get an xml file for each row. i am imagining I would do that with a for loop and counter. Here is the code I have....please assist me in the right direction:

    PHP Code:
    <?php
    // output basic header info and beginning of xml tags
    $output "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    $output .= "<Assets>";

    $result_story mysql_query"Select story.storyid, story.created, story.startdate, story.headline, story.subhead, story.byline, story.body, story.comments, story.isactive, story.keepalive, story.fromprint, story.siteid, topic.topicname FROM story, storytopic, topic WHERE story.isactive = 'Y' AND story.storyid=storytopic.storyid AND storytopic.topicid = topic.topicid"$dbhandle );
    $totalrows mysql_num_rows($result_story);

    $i=0;

    for(
    $i=0$i <= $totalrows$i++){
         
    $row mysql_fetch_assoc($result_story[$i]);
         
    $output .= "<Asset>";
        
    $output .= "<AssetType>Story</AssetType>";
        
    $output .= "<Fields>";
              foreach(
    $row as $name=>$value){
              
    $output .= "<Field>";
                
    $output .= "<Name>" .htmlentities($name);
                
    $output .= "</Name>";
                
    $output .= "<Values>";
                
    $output .= "<Value>" .htmlentities($value)."</Value>";
                
    $output .= "</Values>";
                
    $output .= "</Field>";
                                
              }
          
    $output .= "</Fields>";    
          
    $output .= "</Asset>";      
    }
    $output .= "</Assets>";
           
    // tell the browser what kind of file is come in
    header("Content-type: text/xml");
    // print out XML that describes the schema
    echo $output;

    // close the connection
    mysql_close($dbhandle);

    ?>

  10. #10
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok....i am on my third bottle now....absolutely driving me up the wall. Here is my dilemma. I need <Asset> to be the root, I can not have it outside of my loop, because i need that to be the root of every record. Then I need to loop through the records, and get every row, and write them to a file. Everytime I place <Asset> inside my "outer loop" it comes up with "XML JUNK Error." I can get it to echo fine (see code below) with the exception of the fact that it echos all records in one file. I need separate files for each record. How would I change it to loop through to accommodate my situation.

    Code:

    PHP Code:

    // output basic header info and beginning of xml tags
    $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

    $output .= "<Asset>";

    $result_story mysql_query"Select story.storyid, story.created, story.startdate, story.headline, story.subhead, story.byline, story.body, story.comments, story.isactive, story.keepalive, story.fromprint, story.siteid, topic.topicname FROM story, storytopic, topic WHERE story.isactive = 'Y' AND story.storyid=storytopic.storyid AND storytopic.topicid = topic.topicid LIMIT 0,5000"$dbhandle );
    $totalrows mysql_num_rows($result_story);

    $i=0;

    while(
    $row mysql_fetch_array($result_storyMYSQL_ASSOC)) {
                 
    //             $output .= "<Asset>";
                
    $output .= "<AssetType>Story</AssetType>";
                
    $output .= "<Fields>";
             foreach(
    $row as $name=>$value){
                  
    $output .= "<Field>";
                        
    $output .= "<Name>" .htmlentities($name);
                        
    $output .= "</Name>";
                        
    $output .= "<Values>";
                        
    $output .= "<Value>" .htmlentities($value)."</Value>";
                        
    $output .= "</Values>";
                        
    $output .= "</Field>";
                }
          
    $output .= "</Fields>";    
    //      $output .= "</Asset>";

    //  This is commented out to debug.      
    //      $file_name = $row["storyid"] .".xml";
    //      $fh=fopen($file_name,"w");
    //      fwrite($fh,utf8_encode($output));
    //    fclose($fh);
    //    chmod($file_name,0777);
            
    }

    $output .= "</Asset>";       
     
     
    // print out XML that describes the schema
    echo $output;

    // close the connection
    mysql_close($dbhandle);

  11. #11
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Something like this?
    Code php:
    $result_story = mysql_query( "Select story.storyid, story.created, story.startdate, story.headline, story.subhead, story.byline, story.body, story.comments, story.isactive, story.keepalive, story.fromprint, story.siteid, topic.topicname FROM story, storytopic, topic WHERE story.isactive = 'Y' AND story.storyid=storytopic.storyid AND storytopic.topicid = topic.topicid LIMIT 0,5000", $dbhandle );
     
    $totalrows = mysql_num_rows($result_story);
     
    $i=0;
    while($row = mysql_fetch_array($result_story, MYSQL_ASSOC)) {
             $output = "<?xml version="1.0\" encoding=\"UTF-8\"?>";
             $output .= "<Asset>";
             $output .= "<AssetType>Story</AssetType>";
             $output .= "<Fields>";
             foreach($row as $name=>$value){
                        $output .= "<Field>";
                        $output .= "<Name>" .htmlentities($name);
                        $output .= "</Name>";
                        $output .= "<Values>";
                        $output .= "<Value>" .htmlentities($value)."</Value>";
                        $output .= "</Values>";
                        $output .= "</Field>";
             }
            $output .= "</Fields>";    
     
            $output .= "</Asset>";
     
            $file_name = $row["storyid"] .".xml";
            $fh=fopen($file_name,"w");
            fwrite($fh,utf8_encode($output));
            fclose($fh);
            chmod($file_name,0777);
     
    }
    mysql_close($dbhandle);

    Keep in mind that outputting everything to the browser like that for debug purposes will always give you an error. You're dealing with several separate files which are never to be output to the browser in one bite.
    Saul

  12. #12
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok...tried it, here's the new error:

    XML Parsing Error: no element found
    Line 1, Column 1

    This most likely has to do with not finding that "root" element....any suggestions?

  13. #13
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wait, I think that may have done it daemon. i am confirming with the company that will be utilizing this, but it looks like it conforms to what they want. Thank you so much.

  14. #14
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Sure
    Saul


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
  •