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:
- load xml file in yii (4)
- xampp clientaccesspolicy xml (4)
- xml reading php (4)
- yii load xml file (3)
- enable simple_load_xml file() in local server (2)
- doest not have rest info xml available (2)
- php simple html dom removenode (2)
- simple_load_xml (2)
- autosuggest searching data xml as2 (2)
- wordpress import xml 500 error (2)
- simplexml load string not working in yii curl (1)
- simple_load_xml example (1)
- simplehtml remove node (1)
- simple_load_xml html (1)
- simple_load_xml php example (1)
- tutorial simplexml php dropdown -sql -asp (1)
- where to put crossdomain xml in xampp (1)
- yii multiple dropdownlist (1)
- xml xampp (1)
- xmlmadesimple (1)
- yii country dropdown list (1)
- yii curl post (1)
- yii dialog box onchange (1)
- serach from xml file in yii (1)
- remove child simple html dom (1)
- problem simple_load_xml (1)
- combobox onchange mysql (1)
- could not connect too many connections close connection (1)
- create a object on util in yii (1)
- crossdomain policy xxampp (1)
- crossdomain xml en XAMPP (1)
- how to pass textbox value on server side code of javascript alert message (1)
- how to validate textbox using jqueryn symfony (1)
- ie9 crossdomain (1)
- insert emotion jquery (1)
- jquery insert emotion (1)
- new childnode javascript (1)
- nichost browser caching (1)
- php getxmlvalue (1)
- php imple xml string (1)
You will also be interested in ,
- Pass PHP Value To Javascript
- Learn CSS Basics Very Simple Tutorial
- Single and Multiple selection listbox
- HTML E-mail Using PHP
- Close Event In Javascript For Firefox, IE and Chrome
- Close Event In Javascript Using OnUnload()
- CSS or Table?
- Calendar Script Using Javascript [Updated]
- Change background color using php for a webpage
- Enabling curl on XAMPP for Windows

