PHP and XML
The XML language is a means to organise data so that it may be shared across different websites.
XML is used in a variety of web technologies, including RSS Feeds and Podcasts.
Creating XML is simple. It resembles HTML in appearance, but you create your own tags.
What is an XML Parser?
Your application will require an XML parser, which will allow you to read, update, create, and manipulate XML documents.
XML parsers are divided into two categories in PHP :
- Tree-Based Parsers
- Event-Based Parsers
PHP SimpleXML Parser
It allows us to manipulate and retrieve XML data with ease. SimpleXML is a PHP extension.
It's a tree-based parser that can handle XML documents.
If you know the XML document's structure or layout, SimpleXML makes it simple to extract an element's name, properties, and textual content.
You can iterate through an XML document as if it were a collection of arrays and objects.
SimpleXML uses less lines of code to read text data from an element than the DOM or the Expat parser.
Installation
Because they are part of the PHP core, simplexml functions are available from version 5. These functions can be accessed without installing any software.
PHP SimpleXML - Read From String
This code reads XML data from a string using PHP's simplexml_load_string()
function.
Assume we have a variable that contains XML data, as seen in the example below :
$myXMLData =
"
Tove
Jani
Reminder
Don't forget me this weekend!
";
Example 1 :- The following code demonstrates how to read XML data from a string using the simplexml_load_string()
function :
Tove
Jani
Reminder
Don't forget me this weekend!
";
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>
Output :-
Error Handling Tip : To iterate over all XML errors that occur when loading the document, use the libxml functionality. Following is an example of an attempt to load an unreliable XML string.
Related Links
PHP SimpleXML - Read From File
A file can be read using the PHP function simplexml_load_file()
.
Think of "note.xml" as having the following structure :
Tove
Jani
Reminder
Don't forget me this weekend!
Example :- The following example explains how to read XML data from a file using the simplexml_load_file()
function :
Output :-
Tip : SimpleXML's following chapter explains how to obtain get/retrieve node values from an XML file.
PHP SimpleXML - Get Node Values
Example :- Obtain the node values from the "note.xml" file as follows :
to . "
";
echo $xml->from . "
";
echo $xml->heading . "
";
echo $xml->body;
?>
Output :-
Jani
Reminder
Don't forget me this weekend!
PHP SimpleXML - Get Node Values of Specific Elements
The "books.xml", for example, might look something like this if we had it :
Everyday Italian
Giada De Laurentiis
2005
30.00
Harry Potter
J K. Rowling
2005
29.99
XQuery Kick Start
James McGovern
2003
49.99
Example:- A node value is returned for the title
element in the first and second "book" elements in "books.xml" as seen below :
book[0]->title . "
";
echo $xml->book[1]->title;
?>
Output :-
Harry Potter
Related Links
PHP SimpleXML - Get Node Values - Loop
Example :- title
, author
, year
, and price
node values are obtained by looping over all < book>
elements in "books.xml" :
children() as $books) {
echo $books->title . ", ";
echo $books->author . ", ";
echo $books->year . ", ";
echo $books->price . "
";
}
?>
Output :-
Harry Potter, J K. Rowling, 2005, 29.99
XQuery Kick Start, James McGovern, 2003, 49.99
Learning XML, Erik T. Ray, 2003, 39.95
PHP SimpleXML - Get Attribute Values
Example :- "category"
is the attribute value of first book
element,
and "lang"
is the attribute of second book
title element. Here's how it works :
book[0]['category'] . "
";
echo $xml->book[1]->title['lang'];
?>
Output :-
en
PHP SimpleXML - Get Attribute Values - Loop
Example :- In the "books.xml" file, the following example retrieves the attribute values of the < title>
elements :
children() as $books) {
echo $books->title['lang'];
echo "
";
}
?>
Output :-
en
en-us
en-us