PHP XML Parsers
What is XML?
The XML language is a way to structure data for sharing across websites.
Several web technologies like RSS Feeds and Podcasts are written in XML.
XML is easy to create. It looks a lot like HTML, except that you make up your own tags.
If you want to learn more about XML, please visit our XML tutorial.
What is an XML Parser?
To read and update, create and manipulate an XML document, you will need an XML parser.
In PHP there are two major types of XML parsers:
Tree-Based Parsers
Event-Based Parsers
Tree-Based Parsers
Tree-based parsers holds the entire document in Memory and transforms the XML document into a Tree structure. It analyzes the whole document, and provides access to the Tree elements (DOM).
This type of parser is a better option for smaller XML documents, but not for large XML document as it causes major performance issues.
Example of tree-based parsers:
SimpleXML
DOM
Event-Based Parsers
Event-based parsers do not hold the entire document in Memory, instead, they read in one node at a time and allow you to interact with in real time. Once you move onto the next node, the old one is thrown away.
This type of parser is well suited for large XML documents. It parses faster and consumes less memory.
Example of event-based parsers:
XMLReader
XML Expat Parser
PHP SimpleXML Parser
SimpleXML is a PHP extension that allows us to easily manipulate and get XML data.
The SimpleXML Parser
SimpleXML is a tree-based parser.
SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout.
SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays and objects.
Compared to DOM or the Expat parser, SimpleXML takes a fewer lines of code to read text data from an element.
Installation
From PHP 5, the SimpleXML functions are part of the PHP core. No installation is required to use these functions.
PHP SimpleXML - Read From String
The PHP simplexml_load_string() function is used to read XML data from a string.
Assume we have a variable that contains XML data, like this:
$myXMLData =
"
Tove
Jani
Reminder
Don't forget me this weekend!
";
The example below shows how to use the simplexml_load_string() function to read XML data from a string:
Example
Tove
Jani
Reminder
Don't forget me this weekend!
";
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object");
print_r($xml);
?>
The output of the code above will be:
SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )
Error Handling Tip: Use the libxml functionality to retrieve all XML errors when loading the document and then iterate over the errors. The following example tries to load a broken XML string:
Example
John Doe
john@example.com
";
$xml = simplexml_load_string($myXMLData);
if ($xml === false) {
echo "Failed loading XML: ";
foreach(libxml_get_errors() as $error) {
echo "
", $error->message; } } else { print_r($xml); } ?> The output of the code above will be: Failed loading XML: Opening and ending tag mismatch: user line 3 and wronguser Opening and ending tag mismatch: email line 4 and wrongemail PHP SimpleXML - Read From File The PHP simplexml_load_file() function is used to read XML data from a file. Assume we have an XML file called "note.xml", that looks like this:
Tove
Jani
Reminder
Don't forget me this weekend!
The example below shows how to use the simplexml_load_file() function to read XML data from a file:
Example
The output of the code above will be:
SimpleXMLElement Object ( [to] => Tove [from] => Jani [heading] => Reminder [body] => Don't forget me this weekend! )
Tip: The next chapter shows how to get/retrieve node values from an XML file with SimpleXML!
PHP SimpleXML - Get Node/Attribute Values
SimpleXML is a PHP extension that allows us to easily manipulate and get XML data.
PHP SimpleXML - Get Node Values
Get the node values from the "note.xml" file:
Example
to . "
"; echo $xml->from . "
"; echo $xml->heading . "
"; echo $xml->body; ?> The output of the code above will be: Tove Jani Reminder Don't forget me this weekend! Another XML File Assume we have an XML file called "books.xml", that looks like this:
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
PHP SimpleXML - Get Node Values of Specific Elements
The following example gets the node value of the element in the first and second elements in the "books.xml" file:
Example
book[0]->title . "
"; echo $xml->book[1]->title; ?> The output of the code above will be: Everyday Italian Harry Potter PHP SimpleXML - Get Node Values - Loop The following example loops through all the elements in the "books.xml" file, and gets the node values of the , , , and elements:
Example
children() as $books) {
echo $books->title . ", ";
echo $books->author . ", ";
echo $books->year . ", ";
echo $books->price . "
"; } ?> The output of the code above will be: 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 PHP SimpleXML - Get Attribute Values The following example gets the attribute value of the "category" attribute of the first element and the attribute value of the "lang" attribute of the element in the second element:
Example
book[0]['category'] . "
"; echo $xml->book[1]->title['lang']; ?> The output of the code above will be: COOKING en PHP SimpleXML - Get Attribute Values - Loop The following example gets the attribute values of the elements in the "books.xml" file:
Example
children() as $books) {
echo $books->title['lang'];
echo "
"; } ?> The output of the code above will be: en en en-us en-us PHP XML Expat Parser The built-in XML Expat Parser makes it possible to process XML documents in PHP. The XML Expat Parser The Expat parser is an event-based parser. Look at the following XML fraction:Jani
An event-based parser reports the XML above as a series of three events:
Start element: from
Start CDATA section, value: Jani
Close element: from
The XML Expat Parser functions are part of the PHP core. There is no installation needed to use these functions.
The XML File
The XML file "note.xml" will be used in the example below:
Tove
Jani
Reminder
Don't forget me this weekend!
Initializing the XML Expat Parser
We want to initialize the XML Expat Parser in PHP, define some handlers for different XML events, and then parse the XML file.
Example
";
break;
case "TO":
echo "To: ";
break;
case "FROM":
echo "From: ";
break;
case "HEADING":
echo "Heading: ";
break;
case "BODY":
echo "Message: ";
}
}
// Function to use at the end of an element
function stop($parser,$element_name) {
echo "
"; } // Function to use when finding character data function char($parser,$data) { echo $data; } // Specify element handler xml_set_element_handler($parser,"start","stop"); // Specify data handler xml_set_character_data_handler($parser,"char"); // Open XML file $fp=fopen("note.xml","r"); // Read data while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } // Free the XML parser xml_parser_free($parser); ?> Example explained: Initialize the XML parser with the xml_parser_create() function Create functions to use with the different event handlers Add the xml_set_element_handler() function to specify which function will be executed when the parser encounters the opening and closing tags Add the xml_set_character_data_handler() function to specify which function will execute when the parser encounters character data Parse the file "note.xml" with the xml_parse() function In case of an error, add xml_error_string() function to convert an XML error to a textual description Call the xml_parser_free() function to release the memory allocated with the xml_parser_create() function PHP XML DOM Parser The built-in DOM parser makes it possible to process XML documents in PHP. The XML DOM Parser The DOM parser is a tree-based parser. Look at the following XML document fraction:Jani
The DOM sees the XML above as a tree structure:
Level 1: XML Document
Level 2: Root element:
Level 3: Text element: "Jani"
Installation
The DOM parser functions are part of the PHP core. There is no installation needed to use these functions.
The XML File
The XML file below ("note.xml") will be used in our example:
Tove
Jani
Reminder
Don't forget me this weekend!
Load and Output XML
We want to initialize the XML parser, load the xml, and output it:
load("note.xml");
print $xmlDoc->saveXML();
?>
The output of the code above will be:
Tove Jani Reminder Don't forget me this weekend!
If you select "View source" in the browser window, you will see the following HTML:
Tove
Jani
Reminder
Don't forget me this weekend!
The example above creates a DOMDocument-Object and loads the XML from "note.xml" into it.
Then the saveXML() function puts the internal XML document into a string, so we can output it.
Looping through XML
We want to initialize the XML parser, load the XML, and loop through all elements of the element:
load("note.xml");
$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item) {
print $item->nodeName . " = " . $item->nodeValue . "
"; } ?> The output of the code above will be: #text = to = Tove #text = from = Jani #text = heading = Reminder #text = body = Don't forget me this weekend! #text = In the example above you see that there are empty text nodes between each element. When XML generates, it often contains white-spaces between the nodes. The XML DOM parser treats these as ordinary elements, and if you are not aware of them, they sometimes cause problems.
", $error->message; } } else { print_r($xml); } ?> The output of the code above will be: Failed loading XML: Opening and ending tag mismatch: user line 3 and wronguser Opening and ending tag mismatch: email line 4 and wrongemail PHP SimpleXML - Read From File The PHP simplexml_load_file() function is used to read XML data from a file. Assume we have an XML file called "note.xml", that looks like this:
"; echo $xml->from . "
"; echo $xml->heading . "
"; echo $xml->body; ?> The output of the code above will be: Tove Jani Reminder Don't forget me this weekend! Another XML File Assume we have an XML file called "books.xml", that looks like this:
"; echo $xml->book[1]->title; ?> The output of the code above will be: Everyday Italian Harry Potter PHP SimpleXML - Get Node Values - Loop The following example loops through all the
"; } ?> The output of the code above will be: 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 PHP SimpleXML - Get Attribute Values The following example gets the attribute value of the "category" attribute of the first
"; echo $xml->book[1]->title['lang']; ?> The output of the code above will be: COOKING en PHP SimpleXML - Get Attribute Values - Loop The following example gets the attribute values of the
"; } ?> The output of the code above will be: en en en-us en-us PHP XML Expat Parser The built-in XML Expat Parser makes it possible to process XML documents in PHP. The XML Expat Parser The Expat parser is an event-based parser. Look at the following XML fraction:
"; } // Function to use when finding character data function char($parser,$data) { echo $data; } // Specify element handler xml_set_element_handler($parser,"start","stop"); // Specify data handler xml_set_character_data_handler($parser,"char"); // Open XML file $fp=fopen("note.xml","r"); // Read data while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } // Free the XML parser xml_parser_free($parser); ?> Example explained: Initialize the XML parser with the xml_parser_create() function Create functions to use with the different event handlers Add the xml_set_element_handler() function to specify which function will be executed when the parser encounters the opening and closing tags Add the xml_set_character_data_handler() function to specify which function will execute when the parser encounters character data Parse the file "note.xml" with the xml_parse() function In case of an error, add xml_error_string() function to convert an XML error to a textual description Call the xml_parser_free() function to release the memory allocated with the xml_parser_create() function PHP XML DOM Parser The built-in DOM parser makes it possible to process XML documents in PHP. The XML DOM Parser The DOM parser is a tree-based parser. Look at the following XML document fraction:
"; } ?> The output of the code above will be: #text = to = Tove #text = from = Jani #text = heading = Reminder #text = body = Don't forget me this weekend! #text = In the example above you see that there are empty text nodes between each element. When XML generates, it often contains white-spaces between the nodes. The XML DOM parser treats these as ordinary elements, and if you are not aware of them, they sometimes cause problems.
Comments