Promo Offers XMAS Offers:
KVM VPS @ UP TO 50% OFF for 3 mos. Learn More Dedicated @ UP TO 56% OFF for 1 mo. Learn More
  • Your Cart is Empty
  • Cart
  • Log In

PHP XML Parser

In the begging of the WWW revolution, the HMTL code played an important part, and is still being found in around 90% of all websites. But HTML is focused on how the data will be presented and not on the actual data itself. This is the main reason for the creation of the XML standard - to have a simple data transfer focused standard.

What is XML?

XML (eXtensible Markup Language) is a markup language released in the late 90's. The XML standard was created by W3C to provide an easy-to-use and standardized way to store self-describing data (self-describing data is data that describes both its content and its structure).

What makes XML truly powerful is the international popularity and acceptance it has gained. Many individuals and corporations have put forth lots of efforts and hard work to develop XML interfaces for databases, programming, office applications, mobile phones and more. It is thanks to their enormous contribution that tools exist to perform these conversions from any platform to standardized XML data or to convert XML into a format used by the platform.

Differences between XML and HTML

When you see an XML code you will notice that it is very much like the HTML code. This is, however, not true. In the HTML code, all tags, such as <html>, <body>, <title>, etc, are pre-defined. In XML all the tags used are user-defined, there are no pre-defined ones. Another difference is that an XML file will not do anything on its own. Its only function is to store the data, not manipulate it in any way.

An example of an XML file:

<note>
<to>Mike</to>
<from>Jenny</from>
<heading>Reminder</heading>
<body>Buy milk from the store!</body>
</note>

As you can see in the example above, while the code may look like HMTL, it has nothing to do with it. All of the tags are custom defined, and the actual XML file will not do anything - it's just information surrounded by several tags.

XML and PHP

In order to integrate XML in a PHP application you need to use PHP's XML Parser extension. The extension is included in the PHP core, so there is no need for any additional installations by your web hosting provider or web server administrator. This toolkit lets you parse, but not validate, XML documents. PHP's XML Parser supports three source character encodings also provided by PHP: US-ASCII, ISO-8859-1 and UTF-8. UTF-16 is not supported. The extension lets you create XML parsers and then specify handlers for different XML events. Each XML parser has a few parameters you can adjust.

Simple XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Jake</to>
<from>David</from>
<heading>Reminder</heading>
<body>Call me tonight!</body>
</note>

Simple XML Parser:

<?php
$parser=xml_parser_create();

function char($parser,$data)
{
    echo $data;
}

xml_set_character_data_handler($parser,"char");

$fp=fopen("test.xml","r");

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)));
}

xml_parser_free($parser);


// Will output: Jake David Reminder Call me tonight!

?>

The PHP XML parser uses Expat's XML parser. This parser views the XML file as a series of events and when an event occurs, it calls a specified function to handle it. Due to the fact that it is an event-based parser and it does not validate the XML file, it provides better speed in the XML file manipulation than validating parsers, which makes it more suitable for web applications.