XML & EXPAT
Posted by tutor | Posted in About Websites, PHP Tutorial | Posted on 19-06-2010-05-2008
0
XML
XML is used to describe data. It focuses on what the data is. An XML file describes the structure of the data.
In XML, no tags are predefined. You must define your own tags.
PHP can process XML files with the help of built-in XML Expat Parser.
Expat
To do the manipulation with a file we need to understand it, this is the work of a parser. Similarly to create, read, update and do other manipulations an XML document, you will need an XML parser.
There are two basic types of XML parsers:
- Tree-based parser: This parser transforms an XML document into a tree structure. It analyzes the whole document, and provides access to the tree elements. e.g. the Document Object Model (DOM)
- Event-based parser: Views an XML document as a series of events. When a specific event occurs, it calls a function to handle it. For e.g. the Expat XML parser
Event-based parsers focus on the content of the XML documents, not their structure. Because of this, event-based parsers can access data faster than tree-based parsers.
<sent>Data</sent>
An event-based parser reports the XML above as a series of three events:
- Start element: sent
- Start CDATA section, value: Data
- Close element: from
The XML example above contains well-formed XML. However, the example is not valid XML, because there is no Document Type Definition (DTD) associated with it.
However, this makes no difference when using the Expat parser. Expat is a non-validating parser, and ignores any DTDs.
As an event-based, non-validating XML parser, Expat is fast and small, and a perfect match for PHP web applications.
Note: XML documents must be well-formed or Expat will generate an error.
