PHP SimpleXML Loading XML data into simplexml

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Loading from string

Use simplexml_load_string to create a SimpleXMLElement from a string:

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

Note that or not || must be used here because the precedence of or is higher than =. The code after or will only be executed if $xml finally resolves to false.

Loading from file

Use simplexml_load_file to load XML data from a file or a URL:

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

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

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



Got any PHP Question?