EXPAT PARSER - Php to Xml parser

2017-10-21

The Expat parser is a non-validating (which means it works whether a DTD is associated to document or not), 'event-based' parser. Event-based means it focuses on xml content, not on structure (as tree-based parsers do). An Xml file is analyzed as a list of events: when an event happen, a specific function is called for handling it.

In example, we have the following xml code:

<address>Carnaby Street 45</address>

The code above implies 3 events:

  1. Start element: address
  2. Start CDATA, value: Carnaby Street 45
  3. Closing element: address

Even if Expat is a non-validating parser, xml document must be well formed.

How it works

- Using php, Expat is initialized

parser=xml_parser_create();

- Then, functions for handling xml file events are created.

- Define which elements (events) should be parsed when parser encounter them into xml document and which not.

- An Xml file is loaded for parsing

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

- xml_parser_free() function is called to release the memory allocated with the xml_parser_create() function

Then, Expat parser produce a human readable output.

Download: No download needed as XML Expat parser is part of the PHP core (versions 4+).

Reference source: http://www.w3schools.com/php/php_xml_parser_expat.asp

Submitted by: Stuart