PHP | simplexml_import_dom() Function

The simplexml_import_dom() function is in inbuilt function in PHP which is used to take a node of DOM document and convert it into a SimpleXML node.

Syntax:

SimpleXMLElement simplexml_import_dom( $node, $class_name = "SimpleXMLElement" )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $node: This parameter holds the DOM element node.
  • $class_name: It is optional parameter which holds the class name. If this parameter is used then simplexml_import_dom() function returns the object of specified class. The class should extend the SimpleXMLElement class.

Return Value: This function returns the SimpleXMLElement on success or FALSE on failure.

Below program illustrates the simplexml_import_dom() function in PHP:



Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create an instance of DOMDocument
$dom = new DOMDocument;
  
// Load XML document
$dom -> loadXML('<organization>
        <name>GeeksforGeeks</name>
        <address>Noida India</address>
        <contact>
            <email>abc@geeksforgeeks.org</email>
            <mobile>+91-987654321</mobile>
        </contact>
    </organization>'
);
  
// Use simplexml_import_dom() function to get a
// SimpleXMLElement object from a DOM node
$doc = simplexml_import_dom($dom);
  
// Display the content of XML document
var_dump($doc->contact[0]->email);
var_dump($doc->contact[0]->mobile);
  
?>

chevron_right


Output:

object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(21) "abc@geeksforgeeks.org"
}
object(SimpleXMLElement)#3 (1) {
  [0]=>
  string(13) "+91-987654321"
}

Reference: https://www.php.net/manual/en/function.simplexml-import-dom.php

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.