我如何使用PHP输出xml文件

时间:2014-03-11 19:36:18

标签: php mysql xml

我的网站上有一个MySQL数据库,我想知道如何通过表中的以下列channels的PHP获取XML输出。

我想让xml输出为:

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

这是php代码:

<?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>";
      }
    }
  }
?>

如果你能告诉我怎么做,我真的很感激。请注意;我是PHP的完整菜鸟,任何提供的代码都会有很大的帮助。

编辑:我正在生成一个XML文件以保存在我的网站主机中,但由于收到错误error on line 3 at column 1: Extra content at the end of the document,我无法读取XML文件。

这是XML输出:

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

2 个答案:

答案 0 :(得分:0)

我建议使用XMLWriter。 http://www.php.net/manual/en/ref.xmlwriter.php

我看到你使用mysql ...相反,我建议使用mysqli连接数据库,而不是弃用的mysql。这是改进的扩展。

你可以这样做:

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

祝你好运:)

答案 1 :(得分:0)

下面是一些使用PHP内置类构建XML树的示例代码。这不是您要求的100%,但它演示了如何创建结构,添加子项,添加属性等。使用此片段,您可以从此处获取。我用缩进来帮助我保持树状结构的直线...

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