【问题标题】:Problem with loading remote XML file加载远程 XML 文件时出现问题
【发布时间】:2010-09-04 10:05:19
【问题描述】:

我正在尝试使用 php 加载远程 xml 文件。

这是我的代码:

$doc = new DOMDocument();
$doc->load($this->xml_file);
$file = $doc->getElementsByTagName('file');
$totalFiles = $file->length;
echo $totalFiles;

远程xml文件链接为:

http://localhost/script/index.php?act=xml

这是index.php中的代码:

$xml = '<?xml version="1.0"?><MultimediaGallery>';

$query = mysql_query('SELECT `id`,`image`,`desc` FROM `'
          .confitem('database','prefix').'table` ORDER BY `id` DESC LIMIT '
          .$start.','.$limit);
while($result = mysql_fetch_array($query))
{
    $img = unserialize($result['image']);
    $desc = unserialize($result['desc']);
    $xml .= '<file type="photo"><thumb>'
      .settings('site','url').'/'.OPATH_APPFOLDER.'/'
      .OPATH_UPFOLDER.'/wallpapers/thumbs/'.$img[0].'</thumb><source>'
      .settings('site','url')
      .'/'.OPATH_APPFOLDER.'/'.OPATH_UPFOLDER
      .'/wallpapers/'.$img[0].'</source><description>'
      .$desc[$_SESSION['languagecode']].'</description></file>';
}

$xml .= '</MultimediaGallery>';
header("content-type: text/xml");
echo $xml;

当我直接在浏览器中访问这个 xml 文件链接时,它会以这种样式输出给我的 xml 文件:

<?xml version="1.0"?><MultimediaGallery><file type="photo"><thumb>http://localhost/script/application/data/wallpapers/thumbs/1116205566_42ce0841ab_s.jpg</thumb><source>http://localhost/script/application/data/wallpapers/1116205566_42ce0841ab_s.jpg</source><description>dfdfdfdf</description></file></MultimediaGallery>

当我执行使用 dom 加载 xml 文件的 xml 函数时,出现此错误:

警告:DOMDocument::load() [domdocument.load]:额外内容位于 文件的结尾在 http://localhost/script/index.php, 线:2英寸 C:\AppServ\www\script\application\libraries\wallpapers\multimedia.class.php 第 46 行

为什么会这样?

更新:

我使用 dom 来创建 xml:

$xml = new DOMDocument('1.0');
$root  =  $xml->createElement('MultimediaGallery');
$xml->appendChild($root); 

$query = mysql_query('SELECT `id`,`image`,`desc` FROM `'.confitem('database','prefix').'backgrounds` ORDER BY `id` DESC LIMIT '.$start.','.$limit);
while($result = mysql_fetch_array($query))
 {
  $img   = unserialize($result['image']);
  $desc  = unserialize($result['desc']);
  $element = $xml->createElement('file');
  $root->appendChild($element);
  $attr  =  $xml->createAttribute('type');
  $element->appendChild($attr);
  $attr_text = $xml->createTextNode('photo');
  $attr->appendChild($attr_text);

  $thumb  = $xml->createElement('thumb');
  $element->appendChild($thumb);
  $thumb_text = $xml->createTextNode(settings('site','url').'/'.OPATH_APPFOLDER.'/'.OPATH_UPFOLDER.'/wallpapers/thumbs/'.$img[0]);
  $thumb->appendChild($thumb_text);

  $source  = $xml->createElement('source');
  $element->appendChild($source);
  $source_text = $xml->createTextNode(settings('site','url').'/'.OPATH_APPFOLDER.'/'.OPATH_UPFOLDER.'/wallpapers/'.$img[0]);
  $source->appendChild($source_text);

  $description  = $xml->createElement('description');
  $element->appendChild($description);
  $description_text = $xml->createTextNode($desc[$_SESSION['languagecode']]);
  $description->appendChild($description_text);
 }

header("content-type: text/xml");
echo $xml->saveXML();

但它仍然给我同样的错误。不过我注意到了一些事情,我尝试复制我的输出 xml 并将其保存在一个文件中并使用 dom 解析器读取它,结果是它已成功读取。

但是当我尝试解析 php 文件的 xml 输出时,它会抛出一个错误。

【问题讨论】:

  • 尝试修剪输出?

标签: php xml dom


【解决方案1】:

您的 XML 格式不正确。例如,它有问题。

尽量避免通过连接字符串来生成 XML,因为这会发生。您可以使用 DomDocument 创建 XML 以及读取和操作它。

确保您的 XML 生成脚本中没有前导或尾随空格。

你也应该使用CDATA

【讨论】:

    猜你喜欢
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    相关资源
    最近更新 更多