PHP __construct() Function
Example
Create a SimpleXMLElement object from a string:
<?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->asXML();
?>
Run example »
Definition and Usage
The __construct() function creates a new SimpleXMLElement object.
Syntax
__construct(data,options,data_is_url,ns,is_prefix);
Parameter | Description |
---|---|
data | Required. Specifies 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. Is set by
specifying the option and 1 or 0 (TRUE or FALSE, e.g. LIBXML_NOBLANKS(1)) Possible values:
|
data_is_url | Optional. TRUE specifies that data is a path/URL to an XML document instead of string data. Default is FALSE |
ns | Optional. Specifies a namespace prefix or URI |
is_prefix | Optional. Specifies 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 that represents data |
---|---|
PHP Version: | 5.0.1+ |
PHP Changelog: | PHP 5.1.2: options and data_is_url parameters added. PHP 5.2: ns and is_prefix parameters added. |
More Examples
Assume 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>
Example 1
Create a SimpleXMLElement object from a URL:
<?php
$xml=new SimpleXMLElement("note.xml", 0, TRUE);
echo
$xml->asXML();
?>
Run example »
PHP SimpleXML Reference