PHP and XML

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.

You can also search for these topics, php xml to array with attributes, xml to string in php, php xml to array, Extension for php xml, structure for php xml, php xml web technologies, create php xml.

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
You can also search for these topics, PHP XML Parsers, execute the parser to array using php xml, how to create xml parser in php, working of attributes in php xml parser, xml parser set option php, how to include large files in php xml parser, php xml parser create undefined, what is an xml parser, major types of xml parser using php, Example for xml parser in php.

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.

You can also search for these topics, php simplexml parser, SimpleXML parser PHP extension, how to manipulate parser in php simplexml, get the simplexml parser in php, define simplexml dom parser using php, php simplexml tree based parser, arrays and objects in php simplexml 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.

You can also search for these topics, install php simplexml parser, how to install the php simple xml parser functions, Explain the core of php simple xml parser.

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 =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";

Example 1 :- The following code demonstrates how to read XML data from a string using the simplexml_load_string() function :

<?php
$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>

Output :-

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

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.



You can also search for these topicz, PHP simplexml_load_string() function, how to read from string in php simple xml, reads simpleXML data in php from string, how to read simplexml data from string in php, php simplexml string get arribute, read string return empty object with simplexml in php, Failed loading php simpleXML from string, how to handle error using php simplexml from string, Example for PHP SimpleXML - Read From String.

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 :

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

Example :- The following example explains how to read XML data from a file using the simplexml_load_file() function :

<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
print_r($xml);
?>

Output :-

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

Tip : SimpleXML's following chapter explains how to obtain get/retrieve node values from an XML file.

You can also search for these topics, PHP simplexml_load_file() function, how to read from file in php simple xml, reads simpleXML data in php from file, how to read simplexml data from file in php, php simplexml file get arribute, read file return empty object with simplexml in php, Failed loading php simpleXML from file, how to handle error using php simplexml from file, Example for PHP SimpleXML - Read From file, ways to retrieve php simplexml read from file.

PHP SimpleXML - Get Node Values

Example :- Obtain the node values from the "note.xml" file as follows :

<?php
$xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
echo $xml->to . "<br />";
echo $xml->from . "<br />";
echo $xml->heading . "<br />";
echo $xml->body;
?>

Output :-

Tove
Jani
Reminder
Don't forget me this weekend!

You can also search for these topics, how to use php simplexml get node and attribute values, get all text content using php simplexml, php simplexml get text content, get all child nodes with php simplexml values, php simplexml get element by tag name, manipulate simpleXML data with ease using php, Example for get attribute value with simplexml using php.

PHP SimpleXML - Get Node Values of Specific Elements

The "books.xml", for example, might look something like this if we had it :

<?xml version="1.0" encoding="utf-8"?>
<bookstore>
  <book category="COOKING">
    <title lang="en">Everyday Italian</title>
    <author>Giada De Laurentiis</author>
    <year>2005</year>
    <price>30.00</price>
  </book>
  <book category="CHILDREN">
    <title lang="en">Harry Potter</title>
    <author>J K. Rowling</author>
    <year>2005</year>
    <price>29.99</price>
  </book>
  <book category="WEB">
    <title lang="en-us">XQuery Kick Start</title>
    <author>James McGovern</author>
    <year>2003</year>
    <price>49.99</price>
  </book>
</bookstore>

Example:- A node value is returned for the title element in the first and second "book" elements in "books.xml" as seen below :

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]->title . "<br />";
echo $xml->book[1]->title;
?>

Output :-

Everyday Italian
Harry Potter



You can also search for these topics, PHP SimpleXML to Get Node Values of Specific Elements, get the first and second node values of specific element, Example for simplexml get node values of specific elements using php, node value of the < title> element in the first and second < book> using php simplexml

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" :

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
        echo $books->title . ", ";
        echo $books->author . ", ";
        echo $books->year . ", ";
        echo $books->price . "<br />";
}
?>

Output :-

Everyday Italian, Giada De Laurentiis, 2005, 30.00
Harry Potter, J K. Rowling, 2005, 29.99
XQuery Kick Start, James McGovern, 2003, 49.99
Learning XML, Erik T. Ray, 2003, 39.95

You can also search for these topics, PHP SimpleXML Get Node Values with Loop, how to get node loop values in php simplexml, get node values loop in php simplexml, example loops through all node values with php simplexml, php simplexml elements to get node values loop.

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 :

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
echo $xml->book[0]['category'] . "<br />";
echo $xml->book[1]->title['lang'];
?>

Output :-

COOKING
en

You can also search for these topics, PHP SimpleXML Get attribute Values, how to get attribute values in php simplexml, get attribute values in php simplexml, example for all attribute values with php simplexml, php simplexml elements to get attribute values.

PHP SimpleXML - Get Attribute Values - Loop

Example :- In the "books.xml" file, the following example retrieves the attribute values of the < title> elements :

<?php
$xml=simplexml_load_file("books.xml") or die("Error: Cannot create object");
foreach($xml->children() as $books) {
    echo $books->title['lang'];
    echo "<br />";
}
?>

Output :-

en
en
en-us
en-us

You can also search for these topics, PHP SimpleXML Get attribute Values with Loop, how to get attribute loop values in php simplexml, get attribute values loop in php simplexml, example loops through all attribute values with php simplexml, php simplexml elements to get attribute values loop.