PHP XML SimpleXML

## PHP Basic

PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP String Functions PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals

## PHP Forms

PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete

## PHP Advanced

PHP Arrays Multi PHP Date PHP Include PHP File PHP File Upload PHP Cookies PHP Sessions PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filter

## PHP Database

PHP MySQL Intro PHP MySQL Connect PHP CREATE DB/Table PHP INSERT INTO PHP SELECT PHP WHERE PHP ORDER BY PHP UPDATE PHP DELETE PHP ODBC

## PHP XML

XML Expat Parser XML DOM XML SimpleXML

## PHP and AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

## PHP Reference

PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones

## PHP Quiz

PHP Quiz PHP Certificate

# PHP SimpleXML

« Previous
Next Chapter »

PHP SimpleXML handles the most common XML tasks and leaves the rest for other extensions.

## What is PHP SimpleXML?

SimpleXML is new in PHP 5.

The SimpleXML extension provides is a simple way of getting an XML element's name and text.

Compared to DOM or the Expat parser, SimpleXML just takes a few lines of code to read text data from an XML element.

SimpleXML converts the XML document (or XML string) into an object, like this:

- Elements are converted to single attributes of the SimpleXMLElement object. When there's more than one element on one level, they are placed inside an array
- Attributes are accessed using associative arrays, where an index corresponds to the attribute name
- Text inside elements is converted to strings. If an element has more than one text node, they will be arranged in the order they are found

SimpleXML is fast and easy to use when performing tasks like:

- Reading/Extracting data from XML files/strings
- Editing text nodes or attributes

However, when dealing with advanced XML, you are better off using the Expat parser or the XML DOM.

## Installation

As of PHP 5, the SimpleXML functions are part of the PHP core. No installation is required to use these functions.

## PHP SimpleXML Examples

Assume we have the following XML file, "note.xml":

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

Now we want to output different information from the XML file above:

## Example 1

Output keys and elements of the $xml variable (which is a SimpleXMLElement object):

<?php
$xml=simplexml_load_file("note.xml");
print_r($xml);
?>
Run example »

The output of the code above will be:

SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )

## Example 2

Output the data from each element in the XML file:

<?php
$xml=simplexml_load_file("note.xml");
echo $xml->to . "<br>";
echo $xml->from . "<br>";
echo $xml->heading . "<br>";
echo $xml->body;
?>
Run example »

The output of the code above will be:

Tove
Jani
Reminder
Don't forget me this weekend!

## Example 3

Output the element's name and data for each child node:

<?php
$xml=simplexml_load_file("note.xml");
echo $xml->getName() . "<br>";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br>";
}
?>
Run example »

The output of the code above will be:

note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!

## More PHP SimpleXML

For more information about the PHP SimpleXML functions, visit our PHP SimpleXML Reference.

« Previous
Next Chapter »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE