2010-08-06 61 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