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:

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

Here's what my XML output looks like:

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

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

    I'd start by changing the code shown below to refer to some actual information from the database instead of the string "Information from database" ...

    $channel->appendChild($domtree->createElement('display-name','Information from database'));

      @: Can you please tell me what changes I would need to make that I can create for each xml tag <channel id=""> for each row from mysql database??

      And can you please also tell me how I can make the xml output to make it looks like this?

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

      What my xml output are showing, I want to make some change of these xml source from this:

      <?xml version="1.0" encoding="UTF-8"?>
      <channel>
      <programme>

      To this:

      <?xml version="1.0" encoding="UTF-8" ?>
      <channel id="">
      <programme channel="" start="" stop="">

      Can you please tell me how I can change these xml source using DOMDocument?

      And can you tell me how I can create <channel id=""> tag for each row from mysql?

        You need to walk before you run.

        For starters, stop using the MySQL extension and use instead the newer MySQLI extension (look in my signature).

        I'd recommend altering your script to cycle through the rows in the database first and just output them so you have some vague idea of what you are doing. Then you can figure out how to make your output XML

          Write a Reply...