Sample XML File Used
The following XML data will be contained in the referenced “test_file.xml”:
-
<?xml version=“1.0″ encoding=“ISO-8859-1″?>
-
<cars>
-
<make name=“Ford”>
-
<model>Mustang</model>
-
</make>
-
<make name=“Honda”>
-
<model>Accord</model>
-
</make>
-
</cars>
Loading XML Data Using simplexml_load_file
This function is typically used for loading XML data either from a file, or a remote call. In this example, we will load a flat XML file:
-
<?php
-
// Location of the XML file on the file system
-
$file = ‘test_file.xml’;
-
$xml = simplexml_load_file($file);
-
?>
That’s it. No need to clean anything up, PHP will take care of that for you.
Loading XML Using simplexml_load_string
-
<?php
-
// XML String
-
$xml = ‘
-
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
-
<cars>
-
<make name=”Ford”>
-
<model>Mustang</model>
-
</make>
-
<make name=”Honda”>
-
<model>Accord</model>
-
</make>
-
</cars>
-
‘;
-
$xml = simplexml_load_string($xml);
-
?>
Pretty easy, huh? This function loads an XML string and returns and instance of SimpleXMLElement. You could also directly instantiate the SimpleXMLElement class a similar way, as demonstrated below.
Loading XML Using the SimpleXMLElement Class
-
<?php
-
$xml = ‘
-
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
-
<cars>
-
<make name=”Ford”>
-
<model>Mustang</model>
-
</make>
-
<make name=”Honda”>
-
<model>Accord</model>
-
</make>
-
</cars>
-
‘;
-
$xml = new SimpleXMLElement($xml);
-
?>
It’s your choice on using this method, or simplexml_load_string.
Loading XML Data Using simplexml_import_dom
-
<?php
-
$xml = ‘
-
<?xml version=”1.0″ encoding=”ISO-8859-1″?>
-
<cars>
-
<make name=”Ford”>
-
<model>Mustang</model>
-
</make>
-
<make name=”Honda”>
-
<model>Accord</model>
-
</make>
-
</cars>
-
‘;
-
$dom = new DOMDocument;
-
$dom->loadXML($xml);
-
$xml = simplexml_import_dom($dom);
-
?>
This is handy when you are working with the advanced XML features of DOM. When you’re ready to move from those precocious features, just load the DOM object into simplexml_import_dom, and start programming the easy way!
Incoming search terms:
- xml reading php (4)
- doest not have rest info xml available (2)
- xampp clientaccesspolicy xml (2)
- simple_load_xml (1)
- wordpress import xml 500 error (1)
- could not connect too many connections close connection (1)
- xml xampp (1)
- xmlmadesimple (1)
- yii dialog box onchange (1)
- simplexml load string not working in yii curl (1)
- simplehtml remove node (1)
- remove child simple html dom (1)
- php xml file random selection (1)
- new childnode javascript (1)
- jquery insert emotion (1)
- insert emotion jquery (1)
- how to validate textbox using jqueryn symfony (1)
- crossdomain policy xxampp (1)
- yii multiple dropdownlist (1)
You will also be interested in ,
- Pass PHP Value To Javascript
- HTML E-mail Using PHP
- CSS or Table?
- Close Event In Javascript Using OnUnload()
- Custom Error Pages Using .htaccess
- Learn CSS Basics Very Simple Tutorial
- Text auto slide information box using javascript.
- Adding and removing child nodes using javascript and DOM
- Find Absolute Path Of A File Or Directory Using PHP
- Close Event In Javascript For Firefox, IE and Chrome
