tags:

views:

29

answers:

1

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

+2  A: 

There are are many reasons why you might end up with no output at all. Some I can think of are:

  • There's a parse error in your script and your php version is not configured to show startup errors. see display_startup_errors and/or add some unconditional output to the script (so that if this output is missing you know the script didn't even reach that statement).

  • The script doesn't reach the statement because of some conditions ( `if (false) { ... } ). Again add some output and/or use a debugger to see if the statement is reached.

  • The string contains something that is not valid xml and therefore the libxml parser gives up and simplexml_load_string() returns false. Test the return value and maybe check the errors libxml may have encountered, see http://docs.php.net/function.libxml-use-internal-errors

  • The SimpleXML module isn't present (though in recent versions of php it's enabled by default). Use extension_loaded() and/or function_exists() to test this.

Try it again with a bit more error handling, e.g.

<?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.';

should print something like

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.
VolkerK
i ma getting the xml content through the curl and then load this response as sxe=simlpexml_load_string(response) but when i am printing the sxe blank screen will appear even when i print var_dump(sxe) bool(flase) came as output butwhen i print the xml it display the content
Badshah
The bool(false) indicates that the xml document is not valid/well-formed. And libxml_use_internal_errors/libxml_get_errors should tell you why.
VolkerK