2013-08-31 90 views
0

我正在嘗試讀取我創建的XML文件。我正在使用此代碼,但除了'文件已成功加載'以外沒有任何內容。我試過xpath和其他許多,但它只是不輸出。我的XML可能有問題,因爲我手工創建了該文件,而且以前從未創建過XML文件。PHP SimpleXML不輸出任何內容

最終,我的目標是能夠讀取擴展屬性並獲取描述,類和圖標。

<?php 
if(!$xml = simplexml_load_file('formats.xml')) { 
    echo 'the file was loaded successfully'; 
    print_r($xml); 
} 
else { 
    echo 'the file was not loaded'; 
} 
?> 

這裏是XML:

<?xml version="1.0"?> 
<group class="doc" icon="./.images/doc.png" /> 
    <type extension="doc" description="Legacy Word Document" /> 
    <type extension="docx" description="XML Word Document" /> 
    <type extension="ppt" description="Legacy PowerPoint" /> 
    <type extension="pptx" description="XML PowerPoint" /> 
    <type extension="pps" description="Legacy PowerPoint Show" /> 
    <type extension="ppsx" description="XML PowerPoint Show" /> 
    <type extension="docm" description="Macro Enabled Word Document" /> 
    <type extension="dot" description="Legacy Word Template" /> 
    <type extension="dotx" description="XML Word Template" /> 
    <type extension="dotm" description="Macro Enabled Word Template" /> 
    <type extension="log" description="A Log File" /> 
    <type extension="msg" description="A Message File" /> 
    <type extension="odt" description="OpenOffice Document" /> 
    <type extension="pages" description="Apple Pages Document" /> 
    <type extension="rtf" description="Rich Text Format" /> 
    <type extension="tex" description="LaTeX Document" /> 
    <type extension="wpd" description="Word Perfect Document" /> 
    <type extension="ini" description="Settings File" /> 
    <type extension="cfg" description="Config File" /> 
    <type extension="conf" description="Config File" /> 
    <type extension="nfo" description="Information File" /> 
    <type extension="inf" description="Autorun File" /> 
    <type extension="wps" description="Works Document" /> 
</group> 

<group class="video" icon="./.images/video.png" /> 
    <type extension="asf" description="Advanced Streaming Format" /> 
    <type extension="asx" description="Windows Media Playlist" /> 
    <type extension="avi" description="Audio-Video Interleave" /> 
    <type extension="flv" description="Flash Player Video" /> 
    <type extension="mkv" description="Matroska Video" /> 
    <type extension="mov" description="Apple Movie" /> 
    <type extension="mp4" description="MPEG 4 Video" /> 
    <type extension="mpg" description="MPEG Video" /> 
    <type extension="rm" description="RealMedia Video" /> 
    <type extension="srt" description="Subtitles File" /> 
    <type extension="swf" description="Flash Animation" /> 
    <type extension="vob" description="DVD Video File" /> 
    <type extension="wmv" description="Windows Media Video" /> 
    <type extension="mpeg" description="Motion Picture Experts Video" /> 
</group> 
+1

您的XML無效 - 它沒有一個根元素。由於''包含其他元素,因此它不應該是自封閉的('/>'在開始標記結束時) –

+1

您的邏輯是反向的 - 您輸出消息說明文件在'!$ xml'加載成功時,這意味着它實際上失敗了,因爲前面提到了無效的XML。 –

+0

檢查['libxml_get_errors()'](http://php.net/manual/en/simplexml.examples-errors.php)以查看SimpleXML在抱怨什麼。 –

回答

1

你的XML看起來無效。你需要一個包含所有其餘部分的根元素。此外,你不應該關閉包含其他元素就在開放標籤的元素,如:

<group class="doc" icon="./.images/doc.png" /> 

這應該不是寫爲:

<group class="doc" icon="./.images/doc.png"> 

基本上你的XML應該是這樣的:

<?xml version="1.0"?> 
<extensions> 
    <group class="doc" icon="./.images/doc.png"> 
     <type extension="doc" description="Legacy Word Document" /> 

     ... 

     <type extension="wps" description="Works Document" /> 
    </group> 

    <group class="video" icon="./.images/video.png"> 
     <type extension="asf" description="Advanced Streaming Format" /> 

     ... 

     <type extension="mpeg" description="Motion Picture Experts Video" /> 
    </group> 
</extensions> 

此外,請認識到您檢查文件加載邏輯不正確。當出現相反情況時,您正在輸出文件已成功加載。