1

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>

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?

Edit: When I try this, it will not get the rows from mysql database to put the data in each xml tag channel with other tags display-name, programme-channel, title, sub-title, desc and category.

Here's the source:

<?php

function db_connect()
{
  define('DB_HOST', 'localhost');
  define('DB_USER', 'mybox123');
  define('DB_PASSWORD', 'f00tball');
  define('DB_DATABASE', 'mybox123_tvguide');

  $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());


      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());
      $xml = '<?xml version="1.0" encoding="utf-8"?>
      <tv generator-info-name="www.mysite.com/xmltv"></tv>';

      $my_xml = new SimpleXMLElement($xml);




      while ($row = mysql_fetch_array($result1))
      { 

        foreach($row as $rows) 
        {
          $channel = $my_xml->addChild('channel');
          $channel->addAttribute('id', '');

          $channel->addChild('display-name', $row['channel']);

          $programme = $channel->addChild('programme');
          $programme->addAttribute('channel', $row['channel']);
          $programme->addAttribute('start', $row['start']);
          $programme->addAttribute('stop', $row['stop']); 

          $title = $programme->addChild('title', $row['title']);
          $title->addAttribute('lang', 'en');

          $sub_title = $programme->addChild('sub-title', $row['subtitle']);
          $sub_title->addAttribute('lang', 'en');

          $desc = $programme->addChild('desc', $row['desc']);
          $desc->addAttribute('lang', 'en');

          $category = $programme->addChild('category', $row['category']);
          $category->addAttribute('lang', 'en'); 
        }
      }
      mysql_close();
    }
  }

  // create a dom document with encoding utf8
  $domtree = new DOMDocument($my_xml);
  //finally, save the file
  echo $domtree->saveXML();
  $domtree->save('myChannel.xml');
?>

Here's the xml output:

<?xml version=""?>
0

1 Answer 1

1

From your code:

while ($row = mysql_fetch_array($result1)) 

You should be able to get an array with the following information (or somethings similar):

$rows = array(
array(
    'channel' => 'channel1',
    'start'   => 'start1',
    'stop'   => 'stop1',        
    'title'  => 'title1',
    'subtitle' => 'subtitle1',
    'desc'    => 'desc1',
    'category' => 'category1'
),
array(
    'channel' => 'channel2',
    'start'   => 'start2',
    'stop'   => 'stop2',        
    'title'  => 'title2',
    'subtitle' => 'subtitle2',
    'desc'    => 'desc2',
    'category' => 'category2'
),
);

For getting the XML, I usually use SimpleXMLElement. Example below of how I'm getting the following structure:

<?xml version="1.0" encoding="utf-8"?>
<tv generator-info-name="www.mysite.com/xmltv">
<channel id="">
    <display-name>channel1</display-name>
    <programme channel="channel1" start="start1" stop="stop1">
        <title lang="en">title1</title>
        <sub-title lang="en">subtitle1</sub-title>
        <desc lang="en">desc1</desc>
        <category lang="en">category1</category>
    </programme>
</channel>
<channel id="">
    <display-name>channel2</display-name>
    <programme channel="channel2" start="start2" stop="stop2">
        <title lang="en">title2</title>
        <sub-title lang="en">subtitle2</sub-title>
        <desc lang="en">desc2</desc>
        <category lang="en">category2</category>
    </programme>
</channel>

<?php
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?><tv generator-info-name="www.mysite.com/xmltv"></tv>
XML;

$my_xml = new SimpleXMLElement($xml);

foreach($rows as $row) {
    $channel = $my_xml->addChild('channel');
    $channel->addAttribute('id', '');

    $channel->addChild('display-name', $row['channel']);

    $programme = $channel->addChild('programme');
    $programme->addAttribute('channel', $row['channel']);
    $programme->addAttribute('start', $row['start']);
    $programme->addAttribute('stop', $row['stop']); 

    $title = $programme->addChild('title', $row['title']);
    $title->addAttribute('lang', 'en');

    $sub_title = $programme->addChild('sub-title', $row['subtitle']);
    $sub_title->addAttribute('lang', 'en');

    $desc = $programme->addChild('desc', $row['desc']);
    $desc->addAttribute('lang', 'en');

    $category = $programme->addChild('category', $row['category']);
    $category->addAttribute('lang', 'en');  
}

print_r($my_xml->asXML());
1
  • thank you very much for this, but I have got a problem. It doesn't get the rows from mysql database and it doesn't save it in the xml. The error I'm getting is error on line 1 at column 16: Malformed declaration expecting version. You should post the full source that I should use to get the rows from mysql and output it in each xml tag channel. Have a look at the update post that I post with the source and the xml output.
    – user3208332
    Mar 12, 2014 at 17:49

Your Answer

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