SimpleXMLElement::__construct
(PHP 5, PHP 7)
SimpleXMLElement::__construct — Creates a new SimpleXMLElement object
Description
$data
[, int $options
= 0
[, bool $data_is_url
= FALSE
[, string $ns
= ""
[, bool $is_prefix
= FALSE
]]]] )Creates a new SimpleXMLElement object.
Parameters
-
data
-
A well-formed XML string or the path or URL to an XML document if
data_is_url
isTRUE
. -
options
-
Optionally used to specify additional Libxml parameters.
Note:
It may be necessary to pass
LIBXML_PARSEHUGE
to be able to process deeply nested XML or very large text nodes. -
data_is_url
-
By default,
data_is_url
isFALSE
. UseTRUE
to specify thatdata
is a path or URL to an XML document instead of string data. -
ns
-
Namespace prefix or URI.
-
is_prefix
-
TRUE
ifns
is a prefix,FALSE
if it's a URI; defaults toFALSE
.
Errors/Exceptions
Produces an E_WARNING
error message for each error
found in the XML data and additionally throws an Exception if the XML data
could not be parsed.
Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.
Examples
Note:
Listed examples may include example.php, which refers to the XML string found in the first example of the basic usage guide.
Example #1 Create a SimpleXMLElement object
<?php
include 'example.php';
$sxe = new SimpleXMLElement($xmlstr);
echo $sxe->movie[0]->title;
?>
The above example will output:
PHP: Behind the Parser
Example #2 Create a SimpleXMLElement object from a URL
<?php
$sxe = new SimpleXMLElement('http://example.org/document.xml', NULL, TRUE);
echo $sxe->asXML();
?>
Changelog
Version | Description |
---|---|
5.2.0 | Added the ns and is_prefix parameters. |
5.1.2 | Added the options and data_is_url parameters. |
See Also
- Basic SimpleXML usage
- simplexml_load_string() - Interprets a string of XML into an object
- simplexml_load_file() - Interprets an XML file into an object
- Dealing with XML errors
- libxml_use_internal_errors() - Disable libxml errors and allow user to fetch error information as needed
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-simplexmlelement.construct.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.