Pro PHP- Patterns, Frameworks, Testing and More

(vip2019) #1
CHAPTER 5 ■ WHAT'S NEW IN PHP 6^51

from SimpleXML to full-fledged Document Object Model (DOM) creation. However, these
options have many limitations and, at times, frustrating and confusing APIs
The XMLWriter class is being introduced in PHP 6 to provide a simple, straightforward, and
easy-to-use method for XML document creation. The API for this component has not yet been
fully developed, and it is likely to change. You can find the latest reference information at
http://www.php.net/xmlwriter.
Listing 5-15 is intended as a demonstration of how the API will eventually come together,
but is not intended to be used as exact syntax. This listing builds a basic XHTML document for
the output stream.

Listing 5-15. A Sample XHTML Document

<?php

//Instantiate and set indentation options
$xml = new XMLWriter();
$xml->openURI('php://output');
$xml->setIndentString(' ');
$xml->setIndent(true);

//Start the document and set the DTD
$xml->startDocument('1.0', 'UTF-8');
$xml->startDtd('html','-//W3C//DTD XHTML 1.0 Strict//EN',
'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
$xml->endDtd();

//Create the HTML document

$xml->startElement('html');
$xml->writeAttribute('xmlns', 'http://www.w3.org/1999/xhtml');
$xml->writeAttribute('xml:lang', 'en');
$xml->writeAttribute('lang', 'en');

$xml->startElement('head');
$xml->writeElement('title', 'An example XHTML document.');
$xml->endElement();

$xml->startElement('body');

$xml->writeElement('p', 'Hello, World!');
$xml->startElement('p');
$xml->text('This paragraph contains an inline ');
$xml->startElement('a');
$xml->writeAttribute('href','http://www.example.org');
$xml->text('link.');
$xml->endElement(); //a
$xml->endElement(); //p

McArthur_819-9C05.fm Page 51 Wednesday, February 27, 2008 8:38 AM

Free download pdf