1

I have a MySQL database on my website, and I would like to know how I could get an XML output via PHP of the following column channels in the table.

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 is the php code:

<?php

function db_connect()
{
  define('DB_HOST', 'localhost');
  define('DB_USER', 'myusername');
  define('DB_PASSWORD', 'mypasword');
  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());

      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());
      echo '<?xml version=""1.0"" encoding="UTF-8" ?>';
      echo '<tv generator-info-name="www.mysite.com/xmltv">';
      echo '<channel id="">';
      echo '<display-name></display-name>';
      echo '<programme channel="" start="" stop="">';
      echo '<title lang="en"></title>';
      echo '<sub-title lang="en"></sub-title>';
      echo '<desc lang="en"></desc>';
      echo '<category lang="en"></category>';
      echo '</programme>';
      echo '</channel>';


      while ($row = mysql_fetch_array($result1)) 
      {
        echo "<p id='channels'>".$row["id"]. " " . $row["channels"]. "</p>";
      }
    }
  }
?>

I would really appreciate it if you could tell me how to do this. Please note; I am a complete noob at PHP and ANY supplied code will be of great help.

Edit: I'm generating an XML file to save in my web host, but I can't be able to read the XML file as I'm getting an error error on line 3 at column 1: Extra content at the end of the document.

Here's the XML output:

<?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>
4
  • When you say you want "an XML output" do you mean you want to generate an XML file and save it on the server somewhere? Commented Mar 11, 2014 at 19:40
  • @larsAnders yes i do. I would appreciate if you could tell me how i can save it in my web host. Commented Mar 11, 2014 at 19:41
  • I responded same question here: stackoverflow.com/questions/22338707/… Commented Mar 12, 2014 at 17:36
  • @lachore I have responded to your question, could you please answer it? Commented Mar 12, 2014 at 21:58

2 Answers 2

1

I'd suggest to use XMLWriter. http://www.php.net/manual/en/ref.xmlwriter.php.

I see you use mysql... Instead I'd suggest to use mysqli to connect to the database, instead of the deprecated mysql. It is the improved extension.

You could do something like this:

$xml = new XMLWriter();
$xml->openMemory();
$xml->startDTD('xml');
$xml->endDTD();
    $xml->startElement('tv');
        $xml->startAttribute('generator-info-name');
             $xml->text('www.mysite.com/xmltv'); 
        $xml->endAttribute();
        $xml->startElement('channel');
             $xml->startAttribute('id');
                 $xml->text('');
             $xml->endAttribute();
        $xml->endElement();
    $xml->endElement();
$xml->endElement();

header("Content-type: text/xml; charset=utf-8");
echo $xml->outputMemory();

Good luck with it :)

Sign up to request clarification or add additional context in comments.

1 Comment

Based on your other comments, I'd say you can use the function file_put_contents to write the output to a file instead.
0

Here's some example code using PHP's built in classes to build an XML tree. This isn't 100% of what you're asking for, but it demonstrates how to create the structure, add children, add attributes, etc. Using this snippet, you can get there from here. I use indentation to help me keep the tree structure straight...

// 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 = 'www.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');

6 Comments

@larsAndres: thank you for this, that is what I really want. I have got few problems, the root output is correct but i can't be able to read the myChannel.xml as I'm getting an error error on line 3 at column 1: Extra content at the end of the document there are something missing </tv>. And how do you output for each mysql data in the xml from database using the tag <channel id=information from database>? Please check my update post in my first post.
Instead of this line: $channel = $domtree->appendChild($channel);, add this line: $channel = $tv->appendChild($channel); That will append the channel element to the TV element, making the XML valid. I also edited my answer above.
Thanks, I can see the problem is fixed. I have the <channel> tag in the XML. I want to change that tag to <channel id="">, can you please tell me how i can change it from <channel> to <channel id=""> and how I can output for each row in <channel id=""> from mysql to create for each <channel id=""> tag including with display-name, programme and desc tags?
If you take a hard look at the code I gave you, the answer is already there. There is an example of the very thing you are trying to do - to add an attribute. In fact, this attribute is already teed up for you. It's created, just not appended. Look at how the attribute was generated for $generator_info_name. Follow the example.
Yeah but the code you post doesn't output the data from mysql database and I want to change the tag from <channel> to <channel id'""> with other tags including display-name, programme and desc
|

Your Answer

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