Simplexml_load_file not working for me

Hi,

I am trying to read an XML file and it returns “failed to load”. Please let me know what mistake I am making here. Its a pure simple program. I did it from localhost and with my public host, the XML file and the PHP file both are in the same location.

        <?php
        $xml = simplexml_load_file('xml01.xml') or die("Failed to load");
        echo $xml->event[0]->eventId;
        ?>

This is my XML file saved as "xml01.xml"in the same location as the above PHP :

<?xml version="1.0" encoding="UTF-8"?>
<events>
<event>
<eventId>2818395</eventId>
<eventName>Gala Dinner</eventName>
<eventType>FOOD</eventType>
</event>
</events>

Please help.

Isaac

Hi risuresh welcome to the forum

Might it be a “0 vs. boolean false” gotcha?
http://php.net/manual/en/function.simplexml-load-file.php

Warning

This function may return Boolean FALSE , but may also return a non-Boolean value which evaluates to FALSE . Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Thanks It works. The problem was (I guess), my XML had an element which is a URL and its a big file having 10 elements each with around 100 records.
Do you think if an XML has a URL as an element, which will cause problems in loading using “simplexml_load_file” ?
Thank you.

simple_xml_load() will load the entire file into memory. You will want to partially read the file into memory and process it for large XML files.

XMLReader is more appropriate for parsing large files.

1 Like

AFAIK, XML elements can be just about anything, but I’ve never seen one like <http://example.com>something</http://example.com>. I’m assuming that would not be valid XML and could indeed be a problem. I wouldn’t know how to even begin doing that.

If you mean value eg. <resource>http://example.com</resource> I don’t think that would cause any problems in terms of valid XML

1 Like

Thank you.

My XML file was just like your 2nd node and therefore it should not cause any issues. Anyhow, I will try with a simple XML with just 5 records with a URL and see how it works. Thanks for the information. Appreciated.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.