php xml function requirements

Bad*_*hah 1 php xml

hi i am using the xml function simplexml_load_string for reading the xml string but there is no any output of this function i also use dom function but the same response of this. is there any another method of reading the xml? or is there any modification require on server to enable these function

Vol*_*erK 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.';
Run Code Online (Sandbox Code Playgroud)

应该打印像

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.
Run Code Online (Sandbox Code Playgroud)