如何在运行时在php服务器中生成XML文件


how to generate a XML file in php server at run time

当我运行该程序时,服务器在第24行,第4列有错误,我不理解这个错误:"不匹配XML标签"请检查这个。下面的代码是:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <track>
    <path>song1.mp3</path>
    <title>Track 1 - Track Title</title>
  </track>
  <track>
    <path>song2.mp3</path>
    <title>Track 2 - Track Title</title>
  </track>
  <track>
    <path>song3.mp3</path>
    <title>Track 3 - Track Title</title>
  </track>
  <track>
    <path>song4.mp3</path>
    <title>Track 4 - Track Title</title>
  </track>
  <track>
    <path>song5.mp3</path>
    <title>Track 5 - Track Title</title>
  </track>
  <track>
    <path>song6.mp3</path>
    <title>Track 6 - Track Title</title>
  </track>
  <track>
    <path>song7.mp3</path>
    <title>Track 7 - Track Title</title>
  </track>
  <track>
    <path>song8.mp3</path>
    <title>Track 8 - Track Title</title>
  </track>
</xml>

我想使用一些PHP代码,像这样:

<?php
  // Send the headers
  header('Content-type: text/xml');
  header('Pragma: public');
  header('Cache-control: private');
  header('Expires: -1');
  echo "<?xml version='"1.0'" encoding='"utf-8'"?>";
  echo '<xml>';
  // echo some dynamically generated content here
  /*
     <track>
        <path>song_path</path>
        <title>track_number - track_title</title>
     </track>
  */
  echo '</xml>';
?>

我很确定你的错误来自使用<xml>作为实体,看看这个工作示例,希望它有帮助。

<?php 
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8"?>
<tracks>
    <track>
         <path>song1.mp3</path>
         <title>Track 1 - Track Title</title>
    </track>
    <track>
         <path>song2.mp3</path>
         <title>Track 2 - Track Title</title>
    </track>
    <track>
         <path>song3.mp3</path>
         <title>Track 3 - Track Title</title>
    </track>
    <track>
         <path>song4.mp3</path>
         <title>Track 4 - Track Title</title>
    </track>
    <track>
         <path>song5.mp3</path>
         <title>Track 5 - Track Title</title>
    </track>
    <track>
         <path>song6.mp3</path>
         <title>Track 6 - Track Title</title>
    </track>
</tracks>';
?>