PHP-XML Read File Example

PHP-XML Read File Example

Posted in : PHP Posted on : January 21, 2011 at 6:30 PM Comments : [ 0 ]

This section contains the detail about the reading xml file in PHP.

PHP-XML Read File Example:

Through this example, we will read xml file if file exist, otherwise give a message for the user.

First of all we will load the xml file using "simplexml_load_file("xmlfilename.xml")" function.

The simplexml_load_file() function loads an XML document into an object. This function returns FALSE on failure.

Syntax :

simplexml_load_file(file, class, options, ns, is_prefix) ;

 file  : required specifies the XML document to use

class : optional specifies the class of the new object

options : optional

ns : optional

is_prefix : optional

After load the xml file the php print_r() method print the child of the root element in the file.

Example :

if (file_exists('root.xml'))
$xml_file = simplexml_load_file('root.xml');
exit('root.xml cannot open.');

The root.xml file is

<?xml version="1.0"?>
<child1>clied1 text</child1>
<child2>clied2 text</child2>

After running this example user see the output as:


SimpleXMLElement Object ( [child1] => clied1 text
                                              [child2] => clied2 text )

Note: Before running this example user create a root.xml file.

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics