如何在xml中的每个标记之后添加换行符

时间:2014-03-14 17:21:40

标签: php xml

我正在使用我的php脚本生成XML以将XML文件保存在我的Web主机中。我想做一些换行。我需要在</channel>之后为每个标记添加换行符,但是对于xml中的每一行都不会中断。

这里是xml输出:

<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.mysite.com/xmltv">
<channel id="ABC FAMILY">
   <display-name>ABC FAMILY</display-name>
   <programme channel="ABC FAMILY" start="s1" stop="e1">
       <title lang="en">program title</title>
       <sub-title lang="en">sub title</sub-title>
       <desc lang="en">program description1</desc>
       <category lang="en">some category</category>
   </programme>
</channel> 
\n

<channel id="CBS">
   <display-name>CBS</display-name>
   <programme channel="CBS" start="s1" stop="e1">
       <title lang="en">program title</title>
       <sub-title lang="en">sub title</sub-title>
       <desc lang="en">program description1</desc>
       <category lang="en">some category</category>
   </programme>
</channel> 
\n





<?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, streams FROM tvguide";
      $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());


      while ($row = mysql_fetch_array($result1))
      {
        // fake some example data. the actual data would be retrieved from a database query
        $data[] = array('channel_id'=>$row['channels'],
        'display_name'=>$row['channels'],
        'program_id'=>123,'start'=>'s1','stop'=>'e1',
        'title'=>'program title',
         'sub_title'=>'sub title',
         'description'=>'program description1',
         'category'=>'some category');



        // build the xml    
        $xml = '<?xml version="1.0" encoding="UTF-8" ?>
<tv generator-info-name="www.mysite.com/xmltv">';
        $last_channel = null; // used to detect when the channel changes
        foreach($data as $arr)
        {
            if($last_channel != $arr['channel_id'])
            {
                // the channel changed
                if($last_channel != null){
                    // not the first channel, close out the previous channel
                    $xml .= '
</channel> \n
';
                }
                // start a new channel
                $xml .= "
<channel id=\"{$arr['channel_id']}\">";
                $xml .= "
   <display-name>{$arr['display_name']}</display-name>";
                $last_channel = $arr['channel_id'];
            }
            // output the program info under each channel
            $xml .= "
   <programme channel=\"{$arr['channel_id']}\" start=\"{$arr['start']}\" stop=\"{$arr['stop']}\">";
            // i don't see a program id in this definition, but it likely needs one
            $xml .= "
       <title lang=\"en\">{$arr['title']}</title>";
            $xml .= "
       <sub-title lang=\"en\">{$arr['sub_title']}</sub-title>";
            $xml .= "
       <desc lang=\"en\">{$arr['description']}</desc>";
            $xml .= "
       <category lang=\"en\">{$arr['category']}</category>";
            $xml .= "
   </programme>";
        }
        if($last_channel != null)
        {
            // close out the previous channel if any
            $xml .= '
</channel>';
        }
     }

   }
   $xml .= '
</tv>';
   // output the xml to the browser in this example, write $xml to a file here...
   header("Content-Type: text/xml");
   echo $xml;
   $handle = fopen("myChannel.xml", "w"); 
   fwrite ($handle, $xml);
 }
?>

请告诉我如何在</channel>代码后的每一行中断?

1 个答案:

答案 0 :(得分:1)

\n不适用于单引号。改为使用双引号:

$xml .= "
</channel> \n
";