Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2008
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Generating XML using PHP issues

    Hi all,

    I need some of your help, I'm working on my PHP script to create the XML document with encoding utf8 so I can generate the XML file to allow me to save the XML file in my web host.


    I want to make the xml output to something like this:

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <tv generator-info-name="www.mysite.com/xmltv">
    <channel id="">
       <display-name>Information from database</display-name>
       <programme channel="Information from database" start="" stop="">
           <title lang="en"></title>
           <sub-title lang="en">
           </sub-title>
           <desc lang="en"></desc>
           <category lang="en"></category>
       </programme>
    </channel>


    Here's what my XML output looks like:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <tv generator-info-name="www.mysite.com/xmltv"><channel><display-name>Information from database</display-name><programme/><desc/></channel></tv>

    Here's the current code:

    Code:
    <?php
    
    function db_connect()
    {
      define('DB_HOST', 'localhost');
      define('DB_USER', 'myusername');
      define('DB_PASSWORD', 'mypassword');
      define('DB_DATABASE', 'mydbname');
          
      $errmsg_arr = array();
      $errflag = false;
      $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
      
      if(!$link) 
      {
        die('Failed to connect to server: ' . mysql_error());
      }
    
      $db = mysql_select_db(DB_DATABASE);
      if(!$db) 
      {
        die("Unable to select database");
      }
    }
    db_connect();
    
      function clean($var)
      {
        return mysql_real_escape_string(strip_tags($var));
      } 
      $channels = clean($_GET['channels']);
      $id = clean($_GET['id']);
      
      if($errflag) 
      {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        echo implode('<br />',$errmsg_arr);
      }
      else 
      {
        $insert = array();
      
        if(isset($_GET['channels'])) 
        {
          $insert[] = 'channels = \'' . clean($_GET['channels']) .'\'';
        }
        if(isset($_GET['id'])) 
        {
          $insert[] = 'id = \'' . clean($_GET['id']) . '\'';
        }
        
        
        if($channels && $id) 
        {
          $qrytable1="SELECT id, channels, links FROM tvguide WHERE channels='$channels' && id='$id'";
          $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
          echo '<?xml version="1.0" encoding="UTF-8" ?>
    <tv generator-info-name="www.mysite.com/xmltv">
    <channel id="">
       <display-name></display-name>
       <programme channel="" start="" stop="">
          <title lang="en"></title>
          <sub-title lang="en"></sub-title>
          <desc lang="en"></desc>
          <category lang="en"></category>
       </programme>
    </channel>
    </tv>';
    	
          while ($row = mysql_fetch_array($result1))
          {
            
          }
          mysql_close();
        }
        else if(!$channels && ! $id) 
        {
          $qrytable1="SELECT id, channels, links, streams FROM tvguide";
          $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
    
    	
          while ($row = mysql_fetch_array($result1)) 
          {
            
          }
          mysql_close();
        }
      }
      // create a dom document with encoding utf8
      $domtree = new DOMDocument('1.0', 'UTF-8');
    
      // create a root element of the xml tree
      $tv = $domtree->createElement('tv');
    
        //create attributes for element
        $generator_info_name = $domtree->createAttribute('generator-info-name');
        $generator_info_name->value = 'mysite.com/xmltv';
        //append attribute
        $tv->appendChild($generator_info_name);
        // append element to the doc
        $tv = $domtree->appendChild($tv);
    
        //add a channel as a child of the root
        $channel = $domtree->createElement('channel');
        $channel_id = $domtree->createAttribute('id');
        $channel_id->value = '""';
        $channel = $tv->appendChild($channel);
    
            //append children to channel
            $channel->appendChild($domtree->createElement('display-name','Information from database'));
            $channel->appendChild($domtree->createElement("programme"));
            $channel->appendChild($domtree->createElement('desc'));
    
        //finally, save the file
        echo $domtree->saveXML();
        $domtree->save('myChannel.xml');
    ?>


    Do you know how I can make the same XML output as the first code?

    And how I can output for each data from mysql database to put it in each channel tag and I want to add the tags under the channel tag including the display-name, programme-channel, title, sub-title, desc and category tags when I output for each data from mysql?

    Any advise would be much appreciated.

    Thanks in advance
    Last edited by mark103; 03-12-2014 at 12:14 AM.

  2. #2
    Regular Coder
    Join Date
    Sep 2008
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    does anyone know how I can make the same xml source as the other xml source i want to make it the same?

  3. #3
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    6,282
    Thanks
    30
    Thanked 863 Times in 861 Posts
    This should do it. You might need to play with it a little. Also you can combine the strings to cut down on the "fwrite ($handle , " Like the first two would be:
    fwrite ($handle , '<?xml version="1.0" encoding="UTF-8" ?>'."\r\n".'<tv generator-info-name="www.mysite.com/xmltv">'."\r\n");
    PHP Code:
    <?php
    /*   This is what the DB read provides, I used it to fill in the information   */
    $row[0] = 5;
    $row[1] = "display_name";
    $row[2] = "programme_channel";
    $row[3] = "7:00 PM";
    $row[4] = "9:00 PM";
    $row[5] = "title";
    $row[6] = "sub_title";
    $row[7] = "desc";
    $row[8] = "category";
    /*   You don't need the above, but you do need these two   */
    $stable_id $row[0];
    $begin "start";

    /*   Here starts the xml file    */
    $handle fopen("television.xml""w");
    fwrite ($handle '<?xml version="1.0" encoding="UTF-8" ?>'."\r\n");
    fwrite ($handle '<tv generator-info-name="www.mysite.com/xmltv">'."\r\n");

    while (
    $row mysqli_fetch_array($result){
        if(
    $stable_id == $row[0]){
            if(
    $begin == "start"){fwrite ($handle '<channel id="'.$row[0].'">'."\r\n");$begin="over";echo"here";}
        }else{
            
    fwrite ($handle "</channel>"."\r\n".'<channel id="'.$row[0].'">'."\r\n");
            
    $stable_id $row[0];
        }
        
    fwrite ($handle "<display-name>$row[1]</display-name>"."\r\n");
        
    fwrite ($handle '<programme channel="'.$row[2].'" start="'.$row[3].'" stop="'.$row[4].'">'."\r\n");
        
    fwrite ($handle '<title lang="en">'.$row[5].'</title>'."\r\n");
        
    fwrite ($handle '<sub-title lang="en">'.$row[6].'</sub-title>'."\r\n");
        
    fwrite ($handle '<desc lang="en">'.$row[7].'</desc>'."\r\n");
        
    fwrite ($handle '<category lang="en">'.$row[8].'</category>'."\r\n");
        
    fwrite ($handle "</programme>"."\r\n");
    }
    fwrite ($handle "</channel>"."\r\n");
    fwrite ($handle "</tv>"."\r\n");
    fclose($handle);
    ?>
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  4. #4
    Regular Coder
    Join Date
    Sep 2008
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    @Sunfighter, thank you for posting the example source. There's a problem, the code you posted are in a bit of a mess.

    You did not use my code to add your code which is pathetic, I want to use my code to print the xml source in the php page and then at the end of the php I want to save the xml file called 'myChannel.xml'. You start to write the source at the top of the page which is no good. I want to write the xml file at the end of the php like mine.

    However, I get an error:

    This page contains the following errors:

    error on line 15 at column 95: EntityRef: expecting ';'
    Below is a rendering of the page up to the first error.




    Here's the xml output:

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <tv generator-info-name="www.mysite.com/xmltv">
    <channel>
    <channel id="101">
        <display-name>ABC FAMILY</display-name>
        <programme channel=""">
           <title lang="en"></title>
           <sub-title lang="en"></sub-title>
           <desc lang="en"></desc>
           <category lang="en"></category>
        </programme>
    <channel>
    <channel id="102">
        <display-name>CBS</display-name>
        <programme channel="" start="" stop="">
           <title lang="en"></title>
           <sub-title lang="en"></sub-title>
           <desc lang="en"></desc>
           <category lang="en"></category>
        </programme>
    <channel>


    Here's the php code:

    Code:
    <?php 
    
    function db_connect()
    {
      define('DB_HOST', 'localhost');
      define('DB_USER', 'myusername');
      define('DB_PASSWORD', 'mydbpassword');
      define('DB_DATABASE', 'mydbname');
    
      $errmsg_arr = array();
      $errflag = false;
      $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    
      if(!$link) 
      {
        die('Failed to connect to server: ' . mysql_error());
      }
    
      $db = mysql_select_db(DB_DATABASE);
      if(!$db) 
      {
        die("Unable to select database");
      }
    }
          db_connect();
          
          /*   This is what the DB read provides, I used it to fill in the information   */ 
          $row[0] = 5; 
          $row[1] = "display_name"; 
          $row[2] = "programme_channel"; 
          $row[3] = "7:00 PM"; 
          $row[4] = "9:00 PM"; 
          $row[5] = "title"; 
          $row[6] = "sub_title"; 
          $row[7] = "desc"; 
          $row[8] = "category"; 
          /*   You don't need the above, but you do need these two   */ 
          $stable_id = $row[0]; 
          $begin = "start"; 
    
          /*   Here starts the xml file    */ 
          $handle = fopen("myChannel.xml", "w"); 
          fwrite ($handle , '<?xml version="1.0" encoding="UTF-8" ?>'."\r\n"); 
          fwrite ($handle , '<tv generator-info-name="www.mysite.com/xmltv">'."\r\n"); 
    
          $qrytable1="SELECT id, channels, links, streams FROM tvguide";
          $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
    
    
          while ($row = mysql_fetch_array($result1)) 
          {
             if($stable_id == $row[0])
             { 
                if($begin == "start")
                {
                   fwrite ($handle , '<channel id="'.$row[0].'">'."\r\n");
                   $begin = "over";
                   echo "here";
                } 
             }
             else
             {
                 fwrite ($handle, "<channel>"."\r\n".'<channel id="'.$row[0].'">'."\r\n");  
                 $stable_id = $row[0]; 
             } 
             fwrite ($handle, "    <display-name>".$row[1]."</display-name>"."\r\n"); 
             fwrite ($handle, '    <programme channel="'.$row[2].'" start="'.$row[3].'" stop="'.$row[4].'">'."\r\n"); 
             fwrite ($handle, '       <title lang="en">'.$row[5].'</title>'."\r\n"); 
             fwrite ($handle, '       <sub-title lang="en">'.$row[6].'</sub-title>'."\r\n"); 
             fwrite ($handle, '       <desc lang="en">'.$row[7].'</desc>'."\r\n"); 
             fwrite ($handle, '       <category lang="en">'.$row[8].'</category>'."\r\n"); 
             fwrite ($handle, "    </programme>"."\r\n"); 
          }
          fwrite ($handle , "</channel>"."\r\n"); 
          fwrite ($handle , "</tv>"."\r\n"); 
          fclose($handle);
    ?>




    The code you post are really terrible. Please sort it out and use my code to add yours instead of messing up.

    Thanks!!!!

  5. #5
    Regular Coder
    Join Date
    Sep 2002
    Posts
    539
    Thanks
    0
    Thanked 24 Times in 24 Posts
    You handle it like any other output:
    Code:
    $LineEnding = "\r\n";
    $Output  = '';
    $Output .= '<?xml version="1.0" encoding="UTF-8" ?>' .$LineEnding;
    $Output .= '<tv generator-info-name="www.mysite.com/xmltv">' .$LineEnding;
    
    //--- begin loop ---//
    $Output .= '<channel id="">' .$LineEnding;
    $Output .= '<display-name>Information from database</display-name>' .$LineEnding;
    $Output .= '<programme channel="Information from database" start="" stop="">' .$LineEnding;
    $Output .= '<title lang="en"></title>' .$LineEnding;
    $Output .= '<sub-title lang="en">'</sub-title>' .$LineEnding;
    $Output .= '<desc lang="en"></desc>' .$LineEnding;
    $Output .= '<category lang="en"></category>' .$LineEnding;
    $Output .= '</programme>' .$LineEnding;
    $Output .= '</channel>' .$LineEnding;
    }
    //--- end loop ---//
    Then when you're done just add the output to a file.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    For projects using MediaTypes (MIMETypes) visit E-BAM.net -(updated weekly)

    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  6. #6
    Regular Coder
    Join Date
    Sep 2008
    Posts
    116
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks. I have tried it out but it doesn't work.

    Can you please post the full source using my code?


 

Posting Permissions

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