2010-08-06 60 views
1

嗨,我使用xml函數simplexml_load_string讀取xml字符串,但沒有任何此功能的輸出我也使用dom函數,但是這個相同的響應。 是否有任何其他讀取xml的方法? 或者是否有任何修改要求在服務器上啓用這些功能php xml功能要求

+0

$結果=新的SimpleXMLElement($ XML)其中$ XML是XML字符串,結果是一個PHP對象的字符串。這可能有幫助 – Luke 2010-08-06 14:05:12

+0

請張貼一些代碼給我們看看。你用[SimpleXmlElement :: asXml](http://de2.php.net/manual/en/simplexmlelement.asXML.php)輸出XML – Gordon 2010-08-06 14:06:13

+0

對不起,我可能錯過了解這個問題,我以爲是OP有一個XML字符串,並希望讀取/導航它與PHP – Luke 2010-08-06 14:19:53

回答

5

有很多原因可能導致你根本沒有輸出。有些我能想到的是:

  • 腳本中存在解析錯誤,而您的php版本未配置爲顯示啓動錯誤。請參閱display_startup_errors和/或向腳本添加一些無條件輸出(以便如果缺少此輸出,則知道該腳本甚至沒有達到該聲明)。

  • 由於某些條件(`if(false){...}),腳本沒有達到聲明。再次添加一些輸出和/或使用調試器來查看是否達到了語句。

  • 該字符串包含某些無效的xml,因此libxml解析器放棄並且simplexml_load_string()返回false。測試返回值,可能檢查libxml可能遇到的錯誤,參見http://docs.php.net/function.libxml-use-internal-errors

  • SimpleXML模塊不存在(儘管在最新版本的PHP中默認啓用)。使用extension_loaded()和/或function_exists()來測試。

再試一次,再加上一點錯誤處理,

<?php 
// this is only for testing purposes 
// set those values in the php.ini of your development server if you like 
// but use a slightly more sophisticated error handling/reporting mechanism in production code. 
error_reporting(E_ALL); ini_set('display_errors', 1); 

echo 'php version: ', phpversion(), "\n"; 
echo 'simplexml_load_string() : ', function_exists('simplexml_load_string') ? 'exists':"doesn't exist", "\n"; 

$xml = '<a> 
    >lalala 
    </b> 
</a>'; 

libxml_use_internal_errors(true); 
$doc = simplexml_load_string($xml); 
echo 'errors: '; 
foreach(libxml_get_errors() as $err) { 
    var_dump($err); 
} 

if (!is_object($doc)) { 
    var_dump($doc); 
} 
echo 'done.'; 

應打印像

php version: 5.3.2 
simplexml_load_string() : exists 
errors: object(LibXMLError)#1 (6) { 
    ["level"]=> 
    int(3) 
    ["code"]=> 
    int(76) 
    ["column"]=> 
    int(7) 
    ["message"]=> 
    string(48) "Opening and ending tag mismatch: a line 1 and b 
" 
    ["file"]=> 
    string(0) "" 
    ["line"]=> 
    int(3) 
} 
object(LibXMLError)#2 (6) { 
    ["level"]=> 
    int(3) 
    ["code"]=> 
    int(5) 
    ["column"]=> 
    int(1) 
    ["message"]=> 
    string(41) "Extra content at the end of the document 
" 
    ["file"]=> 
    string(0) "" 
    ["line"]=> 
    int(4) 
} 
bool(false) 
done. 
+0

我可以通過curl獲取xml內容,然後加載此響應爲sxe = simlpexml_load_string(響應),但是當我打印sxe空白屏幕時,即使當我打印var_dump(sxe)bool(flase)作爲輸出,但是當我打印xml時,它顯示內容 – Badshah 2010-08-07 06:23:18

+0

bool(false)表示xml文檔無效/格式良好。而libxml_use_internal_errors/libxml_get_errors應該告訴你爲什麼。 – VolkerK 2010-08-07 06:52:19

+0

乾杯,這幫助加載:) – encodes 2013-12-06 14:35:56