PHP | SimpleXMLElement::__construct() Function
Pre-requisite:XML The __construct() function is an inbuilt function in PHP that is used to create a new SimpleXMLElement object for XML.
Syntax:
SimpleXMLElement::__construct( $data, $options, $data_is_url, $namespace, $is_prefix )
Parameters: This function accepts five parameters as mentioned above and described below:
- $data: It is a required parameter. It specifies a well-formed XML string or the path or URL to an XML document file if $data_is_url is TRUE.
- $options: It is an optional parameter. It specifies additional Libxml parameters. Is set by specifying the option and 1 or 0
- $data_is_url: It is an optional parameter. Its default value is False. If the value of this parameter is True, it specifies that the data is a path/URL to a file of XML document instead of a string.
- $namespace: It is an optional parameter. It specifies the namespace URI or prefix.
- $is_prefix: It is an optional parameter of Boolean type. It specifies True if ns is a prefix and False if ns is a URI. The default value is False.
Return Value: It returns a SimpleXMLElement object that represents the XML structured data
Note: This function is available for PHP 5.0.1 and newer versions.
Example
php
<?php
$user = <<<XML
<user>
<username> user123 </username>
<name> firstname lastname </name>
<phone> +91-XXXXXXXXXX </phone>
<detail> I am John Doe. Live in Kolkata, India. </detail>
</user>
XML;
$xml = new SimpleXMLElement( $user );
echo $xml ->asXML();
?>
|
Output:
user123 firstname lastname +91-XXXXXXXXXX I am John Doe. Live in Kolkata, India.
Output in browser as source:
Example: Loading XML document from file or url. sample.xml
html
<? xml version="1.0"?>
< user >
< username > user123 </ username >
< name > firstname lastname </ name >
< phone > +XX-XXXXXXXXXX</ phone >
< detail > I am John Doe. Live in Kolkata, India. </ detail >
</ user >
|
index.php
php
<?php
$xml = new SimpleXMLElement("sample.xml", 0, TRUE);
echo $xml ->asXML();
?>
|
Output:
user123 firstname lastname +91-XXXXXXXXXX I am John Doe. Live in Kolkata, India.
Output in browser as a source:
Possible values of the parameter $option are:
- LIBXML_COMPACT – Nodes allocation optimization activated
- LIBXML_DTDATTR – This option sets default DTD attributes
- LIBXML_DTDLOAD – Load subset externally
- LIBXML_DTDVALID – Validate the xml document with the DTD
- LIBXML_DOTTED_VERSION – Get libxml version dot separated
- LIBXML_ERR_ERROR – Get errors(recoverable)
- LIBXML_ERR_FATAL – Get errors(fatal)
- LIBXML_ERR_NONE – Get no errors
- LIBXML_ERR_WARNING – Get warnings(simple)
- LIBXML_NOBLANKS – Remove all blank nodes in the XML document
- LIBXML_NOCDATA – Merge new CDATA as text nodes
- LIBXML_NOEMPTYTAG – Expand empty tags
- LIBXML_NOENT – Substitute entities in XML document
- LIBXML_NOERROR – restrict error report
- LIBXML_NONET – Disables network access while loading the XML documents
- LIBXML_NOWARNING – restricts warning reports
- LIBXML_NOXMLDECL – Drops the XML declaration when saving the document
- LIBXML_NSCLEAN – Removes all redundant namespace declarations
- LIBXML_PARSEHUGE – Sets XML_PARSE_HUGE flag, which removes any hardcoded limit from the parser which again affects limits like the size of text nodes and maximum depth of a document
- LIBXML_XINCLUDE – Substitution of implements XInclude
- LIBXML_VERSION – Get the LIBXML version in integer format
Reference: https://www.php.net/manual/en/simplexmlelement.construct.php
Last Updated :
02 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...