2010-09-04 62 views
0

我試圖使用php加載遠程xml文件。加載遠程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功能:

Warning: DOMDocument::load() [domdocument.load]: Extra content at the end of the document in http://localhost/script/index.php , line: 2 in C:\AppServ\www\script\application\libraries\wallpapers\multimedia.class.php on line 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輸出,然後它會引發錯誤。

+0

嘗試修剪輸出嗎? – Robus 2010-09-04 10:08:41

+0

更好地格式化XML – 2010-09-04 10:17:47

回答

0

您的XML格式不正確。例如,它有什麼問題。

儘量避免通過連接字符串來製作XML,因爲這會發生。您可以使用DomDocument製作XML,並閱讀並操作它。

請確保您的XML生成腳本中沒有前導或尾隨空白。

您還應該使用CDATA