0

I keep having an issue where one of my variables, $xml, keeps being mistaken for a boolean when it should be returning an object.

I've tried changing $xml to $data... thinking maybe it was a naming issue.

I have also tried isolating the str() function but that is being recognized as string - which is correct.

I have also tried using gettype(simplexml_load_string(str()); which returns a bool as well.

Please let me know what I might be doing wrong:

    function str() {
        if(file_exists(__DIR__ . '/../' . $clientXML)) {
            return file_get_contents(__DIR__ . '/../' . $clientXML);
        } else {
            return file_get_contents(__DIR__ . '/../xml-skeleton.xml');
        }
    }

    $xml = simplexml_load_string(str());

    echo 'XML IS: ' . gettype($xml);
veryrarecandy
  • 323
  • 1
  • 4
  • 17

1 Answers1

0

simplexml_load_string returns FALSE if its unable to parse the xml string. You can deal/check for the error like this:

<?php
libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'><broken><xml></broken>");
if ($sxe === false) {
    echo "Failed loading XML\n";
    foreach(libxml_get_errors() as $error) {
        echo "\t", $error->message;
    }
}
?>

This is taken from: http://us3.php.net/manual/en/simplexml.examples-errors.php

Manveer Chawla
  • 404
  • 2
  • 6
  • Hrm... at the moment this is returning "Failed loading XML." However, it's not returning any specific errors. – veryrarecandy Feb 18 '14 at 02:18
  • When I use $xml = simplexml_load_file(__DIR__ . '/../' . $clientXML); this seems to work. – veryrarecandy Feb 18 '14 at 02:19
  • I feel like there's something happening with the return in the if/else that's causing some weird behaviors. Not sure what yet. – veryrarecandy Feb 18 '14 at 02:19
  • @JIGGABITS Not entirely sure about this, are the files you are dealing with here huge? Can you try setting the LIBXML_PARSEHUGE flag as mentioned here: http://stackoverflow.com/questions/14950589/simplexml-load-string-errors-on-big-files-occur-on-one-system-but-not-another – Manveer Chawla Feb 18 '14 at 02:53
  • I've updated the return in str() to be `return simplexml_load_string(__DIR__ . '/../' . $clientXML)` -- Now when I try to assign this value to $xml I get a clearer error: __Start tag expected, '<' not found__ – veryrarecandy Feb 18 '14 at 19:47
  • When you say you have changed it to return `return simplexml_load_string(__DIR__ . '/../' .` you mean you have changed it to `return simplexml_load_string(file_get_contents(__DIR__ . '/../' .$clientXML))`. Just wanted to confirm. Also, have you logged and made sure that xml starts with the '<' tag? If yes, may be explicity cast the file_get_contents to string and use? – Manveer Chawla Feb 18 '14 at 21:35
  • No that was not what I used. But I did change it to __return simplexml_load_string(file_get_contents(__DIR__ . '/../' .$clientXML))__ -- After using that I got an error that only said __Failed loading XML__ – veryrarecandy Feb 19 '14 at 19:14