【问题标题】:How do I add line breaks after each tag in the xml如何在 xml 中的每个标记后添加换行符
【发布时间】:2014-03-14 17:21:39
【问题描述】:

我正在编写我的 php 脚本以生成 XML 以将 XML 文件保存在我的网络主机中。我想做一些换行符。我需要在</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);
 }
?>

你能告诉我如何在&lt;/channel&gt; 标记后的每一行中换行吗?

【问题讨论】:

  • Quixrick 可以解决您的问题。虽然我知道您已经编写了代码并且它可能有效,但是当您拥有像 us2.php.net/simplexml 这样的库时,用 php 重新发明轮子没有意义
  • 我同意;在 PHP 中构建、修改或解析 XML 时,PHP 中的 SimpleXML 使事情变得非常容易。

标签: php xml


【解决方案1】:

\n 在单引号中不起作用。改用双引号:

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

【讨论】:

  • 感谢您。我在该行中添加了双引号,但它并没有在&lt;/channel&gt; 标记之后断开该行。它只会使用视图源显示换行符,但不会显示 xml 根目录。你知道为什么吗?
  • 除了写入文件之外,您还试图在网页上显示 XML?当然在 HTML 中,中断是&lt;br&gt; 而不是\n。因此,如果你想两者都做,你有几个选择。您可以设置一个分隔符,然后您可以通过它稍后替换(一个用于网络,一个用于文件),或者您可以只制作 2 个版本。 $xml .= "&lt;/channel&gt;\n"; $xml_web .= "&lt;channel&gt;&lt;br&gt;"; 如果您需要任何帮助,请告诉我。我可能也误解了您要做什么。所以请随时纠正我。
  • 是的,我在我的 php.ini 中显示 xml。我想在&lt;/channel&gt; 之后换行,我尝试使用您建议我使用的代码,但出现错误error on line 132 at column 6: Opening and ending tag mismatch: channel line 0 and tv。我使用的来源`
    ;'。请看我的示例 xml 源图片:imageshack.com/a/img836/1483/wlxo.png
  • 啊,感谢您提供屏幕截图。那么您所看到的是浏览器对 XML 的解释,您无法更改它。如果您转到不同的浏览器并查看相同的页面,它看起来会完全不同。如果你真的想以它的外观显示 XML,你会想使用像 htmlentities 这样的东西。来,在collabedit见我。 collabedit.com/tgvry
  • 哦,好的,谢谢您的帮助。我没有意识到我从我下载的网站上得到了一个 xml,我没有意识到我不能在 xml 中换行,所以还是谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-28
  • 2012-03-14
  • 1970-01-01
  • 2016-06-01
  • 1970-01-01
相关资源
最近更新 更多