PHP SimpleXMLElement::__construct() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
SimpleXML

Definition

In PHP, SimpleXMLElement::__construct() creates a new SimpleXMLElement object from an XML string, so you can read the XML like a normal object.

You do not call it by name. You write new SimpleXMLElement($xml) and PHP runs the constructor for you. If the XML is broken, the constructor throws an Exception instead of returning false.

Syntax

Call SimpleXMLElement::__construct() with the values you need. Start with this line.

PHP
$book = new SimpleXMLElement('<book><title id="1">PHP</title></book>');
CallWhat it does
new SimpleXMLElement($xml)Builds an object from an XML string. This is the most common way to use the constructor.
new SimpleXMLElement($xml, LIBXML_NOCDATA)Passes libxml option flags, here to merge CDATA sections into plain text.
new SimpleXMLElement($path, 0, true)Sets $dataIsURL to true, so the first argument is a file path or URL instead of XML text.

The constructor throws an Exception when the XML cannot be parsed. Wrap it in try and catch, or use simplexml_load_string() if you prefer a false result on failure.

Library declarations
PHP
SimpleXMLElement::__construct(string $data, int $options = 0, bool $dataIsURL = false, string $namespaceOrPrefix = "", bool $isPrefix = false)

Parameters

ParameterDescription
dataRequired. A well-formed XML string, or a path or URL when $dataIsURL is true.
optionsOptional. Libxml option flags such as LIBXML_NOCDATA. The default is 0.
dataIsURLOptional. Set to true when $data is a file path or URL. The default is false.
namespaceOrPrefixOptional. A namespace prefix or URI, used together with $isPrefix. The default is an empty string.
isPrefixOptional. Set to true when $namespaceOrPrefix is a prefix and not a URI. The default is false.

Return Value

Return ValueDescription
SimpleXMLElementThe new object that represents the root element of the XML.
ExceptionThrown when the XML string cannot be parsed.

PHP Examples

Example 1 — Create an Object from XML

Pass a tiny XML string to new SimpleXMLElement and read the title text and the root name.

PHP
<?php
$xml = '<book><title id="1">PHP</title></book>';

$book = new SimpleXMLElement($xml);

echo $book->getName(), PHP_EOL;
echo (string) $book->title, PHP_EOL;

How It Works

  1. The XML string holds one book element with a title inside.
  2. new SimpleXMLElement($xml) runs the constructor and parses the text.
  3. getName() returns the name of the root element, which is book.
  4. The title is cast to a string so echo prints the text PHP.

Example 2 — Invalid XML Throws an Exception

When the XML is broken, the constructor throws. Turn on internal errors to keep the page free of warnings, then catch the exception.

PHP
<?php
libxml_use_internal_errors(true);

try {
    $book = new SimpleXMLElement('<book><title>PHP</book>');
    echo "Parsed", PHP_EOL;
} catch (Exception $e) {
    echo "Invalid XML", PHP_EOL;
}

How It Works

  1. libxml_use_internal_errors(true) stores parse problems instead of printing them.
  2. The title tag is closed by </book>, so the XML is not well formed.
  3. The constructor throws an Exception, and the line that prints Parsed never runs.
  4. The catch block prints a friendly message.

Example 3 — Use the LIBXML_NOCDATA Option

The second argument sets libxml options. Compare the same XML with and without LIBXML_NOCDATA.

PHP
<?php
$xml = '<book><title><![CDATA[PHP]]></title></book>';

$plain = new SimpleXMLElement($xml);
$clean = new SimpleXMLElement($xml, LIBXML_NOCDATA);

echo $plain->title->asXML(), PHP_EOL;
echo $clean->title->asXML(), PHP_EOL;

How It Works

  1. The title text is wrapped in a CDATA section.
  2. The first object uses the default options, so the CDATA section is kept.
  3. The second object passes LIBXML_NOCDATA, which turns CDATA into plain text.
  4. asXML() prints each title element so you can see the difference.

Frequently Asked Questions

It creates a new SimpleXMLElement object from an XML string. You call it with new SimpleXMLElement($xml), and then you can read elements and attributes like object properties.
Both parse an XML string. The constructor throws an Exception when the XML is invalid, while simplexml_load_string() returns false.
Pass the file path as the first argument and set the third argument to true, like new SimpleXMLElement($path, 0, true). For files, simplexml_load_file() is usually shorter.
libxml also prints a warning for each parse error. Call libxml_use_internal_errors(true) before parsing to collect the errors silently.
No. Never pass user input as the first argument when $dataIsURL is true. Load only trusted files, and validate the input before you parse it.

Next: SimpleXMLElement::__toString()

SimpleXMLElement::__toString() returns the text content of an element as a plain string.

SimpleXMLElement::__toString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful