Loading XML data into simplexml

suggest change

Loading from string

Use simplexml_load_string to create a SimpleXMLElement from a string:

$xmlString = "<?xml version='1.0' encoding='UTF-8'?><root/>";
$xml = simplexml_load_string($xmlString) or die("Error: Cannot create object");

Use or, not ||. = binds tighter than or, so the assignment happens first and die runs only when that value is false. || binds tighter than =, so it would assign true or false instead of the element. The string must be a complete document. A declaration alone has no root element.

Loading from file

Use simplexml_load_file to load XML from a file or a URL. simplexml_load_string parses the text you pass. It does not open a path.

$xml = simplexml_load_file("filePath.xml");

$xml = simplexml_load_file("https://example.com/doc.xml");

A remote URL needs allow_url_fopen, which is on by default.

The URL can be of any schemes that PHP supports, or custom stream wrappers.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents