Easy Tutorial
❮ Func Math Atan2 Func Curl_Setopt_Array ❯

PHP __construct() Function

PHP SimpleXML Reference Manual

Example

The function creates a new SimpleXMLElement object and then outputs the content of the body node:

<?php
$note=<<<XML
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML;

$xml=new SimpleXMLElement($note);
echo $xml->body;
?>

Definition and Usage

The __construct() function creates a new SimpleXMLElement object.


Syntax

Parameter Description
data Required. A well-formed XML string or the path or URL to an XML document if data_is_url is TRUE.
options Optional. Specifies additional Libxml parameters. Set by specifying options as 1 or 0 (TRUE or FALSE, e.g., LIBXML_NOBLANKS(1)). Possible values: LIBXML_COMPACT - Activate nodes' optimized configuration (speeds up application)<br> LIBXML_DTDATTR - Set default DTD attributes<br> LIBXML_DTDLOAD - Load external subset<br> LIBXML_DTDVALID - Validate with DTD<br> LIBXML_NOBLANKS - Remove blank nodes<br> LIBXML_NOCDATA - Merge CDATA as text nodes<br> LIBXML_NOEMPTYTAG - Expand empty tags (e.g., <br/> to <br></br>), only effective in DOMDocument->save() and DOMDocument->saveXML() functions<br> LIBXML_NOENT - Substitute entities<br> LIBXML_NOERROR - Do not show error reports<br> LIBXML_NONET - Disable network access while loading documents<br> LIBXML_NOWARNING - Do not show warning reports<br> LIBXML_NOXMLDECL - Drop the XML declaration when saving a document<br> LIBXML_NSCLEAN - Remove redundant namespace declarations<br> LIBXML_PARSEHUGE - Set the XML_PARSE_HUGE flag, which relaxes any hardcoded limit from the parser. This affects limits like maximum depth of the document and limits on the size of text nodes.<br> LIBXML_XINCLUDE - Implement XInclude substitution<br> LIBXML_ERR_ERROR - Get recoverable errors<br> LIBXML_ERR_FATAL - Get fatal errors<br> LIBXML_ERR_NONE - Get no errors<br> LIBXML_ERR_WARNING - Get simple warnings<br> LIBXML_VERSION - Get libxml version (e.g., 20605 or 20617)<br> LIBXML_DOTTED_VERSION - Get dotted libxml version (e.g., 2.6.5 or 2.6.17)
data_is_url Optional. If TRUE, data is the path or URL to an XML document instead of string data. Default is FALSE.
ns Optional. Specifies the namespace prefix or URI.
is_prefix Optional. A boolean value. TRUE if ns is a prefix, FALSE if ns is a URI. Default is FALSE.

Technical Details

Return Value: Returns a SimpleXMLElement object representing data.
PHP Version: 5.0.1+
--- ---
PHP Changelog: PHP 5.1.2: Added options and data_is_url parameters. <br>PHP 5.2: Added ns and is_prefix parameters.
--- ---

More Examples

Suppose we have the following XML file note.xml:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>

<body>Don't forget me this weekend!</body> </note>

❮ Func Math Atan2 Func Curl_Setopt_Array ❯