PHP | SimpleXMLElement::__construct() Function

Pre-requisite:XML

The __construct() function is an inbuilt function in PHP which 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 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 optional parameter. It specifies additional Libxml parameters. Is set by specifying the option and 1 or 0
  • $data_is_url: It is optional parameter. Its default value is False. If value of this parameter is True, it specifies that the data is a path/URL to a file of XML document instead of string.
  • $namespace: It is optional parameter. It specifies the namespace URI or prefix.
  • $is_prefix: It is 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 version.

Example

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document to $user
$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;
  
// Creating new SimpleXMLElement
// object from $user
$xml = new SimpleXMLElement($user);
  
// Printing as XML
echo $xml->asXML();
  
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>

chevron_right


index.php

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Loading XML document from sample.xml to $user
// and creating new SimpleXMLElement object
$xml = new SimpleXMLElement("sample.xml", 0, TRUE);
  
// Printing data as xml document
echo $xml->asXML();
  
?>

chevron_right


Output:

user123 firstname lastname +91-XXXXXXXXXX I am John Doe. Live in Kolkata, India.

Output in browser as source:

Possible values of 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 seperated
  • 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 remove 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 LIBXML version in integer format

Reference: https://www.php.net/manual/en/simplexmlelement.construct.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.