PHP simplexml_import_dom() Function
Definition and Usage
XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The simple XML parser is used to parse Name, attributes and textual content.
The simplexml_import_dom() accepts an object of the class DOMNode as a parameter, converts it into an object of the SimpleXMLElement class and returns it.
Syntax
simplexml_import_dom($filename, [$class_name, $options, $ns, $is_prefix]);
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
node(Mandatory) This is an object of the class DOMNode. |
| 2 |
class_name(Optional) This is a string value to representing the name of the class (sub class of the SimpleXMLElement). If you pass this value, the given XML string is returned as the object of the specified class. |
Return Values
This function returns an object of the SimpleXMLElement class in case of success and returns the boolean value FALSE in case of failure.
PHP Version
This function was first introduced in PHP Version 5 and works in all the later versions.
Example
Following example demonstrates the usage of the simplexml_import_dom() function.
<html>
<head>
<body>
<?php
$doc=new DOMDocument;
$data="<?xml version='1.0' encoding='UTF-8'?>
<Employee>
<Name>Raju</Name>
<Age>25</Age>
<Salary>2000</Salary>
</Employee>";
$doc ->loadXML($data);
$res = simplexml_import_dom($doc);
print_r($res);
?>
</body>
</head>
</html>
This will produce following result −
SimpleXMLElement Object ( [Name] => Raju [Age] => 25 [Salary] => 2000 )
Example
In the following example we are trying to retrieve the SimpleXMLElement object from (of) an XML file with multiple records and retrieve values from it −
Data.xml:
<?xml version="1.0" encoding="utf-8"?>
<Tutorials>
<Tutorial>
<Name>JavaFX</Name>
<Pages>535</Pages>
<Author>Krishna</Author>
<Version>11</Version>
</Tutorial>
<Tutorial>
<Name>CoffeeScript</Name>
<Pages>235</Pages>
<Author>Kasyap</Author>
<Version>2.5.1</Version>
</Tutorial>
</Tutorials>
sample.html
<html>
<head>
<body>
<?php
$doc = new DOMDocument;
$xml = simplexml_load_file("mydata.xml");
$res = simplexml_import_dom($xml);
print("<br>");
foreach($res->children() as $tut) {
print($tut->Name ."<br>");
print($tut->Pages ."<br>");
print($tut->Author ."<br>");
print($tut->Version ."<br>");
print("<br>");
}
?>
</body>
</head>
</html>
This will produce the following output −
JavaFX 535 Krishna 11 CoffeeScript 235 Kasyap 2.5.1
Example
Following example demonstrates the usage of this method with the optional parameter −
<html>
<head>
<body>
<?php
$doc=new DOMDocument;
$str="<Employee>
<Name>Raju</Name>
<Age>25</Age>
<Salary>2000</Salary>
</Employee>";
$doc ->loadXML($str);
$res = simplexml_import_dom($doc, "SimpleXMLElement");
print_r($res);
?>
</body>
</head>
</html>
This will produce following result −
SimpleXMLElement Object ( [Name] => Raju [Age] => 25 [Salary] => 2000 )
Example
<?php
$dom = new domDocument;
$dom->loadXML("<car><local><title>
Title1</title></local><local><title>
Title2</title></local></car>");
$x = simplexml_import_dom($dom);
echo $x->local[1]->title;
?>
This will produce following result −
Title2