XML and PHP [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

XML and PHP [Electronic resources] - نسخه متنی

Vikram Vaswani

نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
تنظیمات قلم


اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
لیست موضوعات
افزودن یادداشت
افزودن یادداشت جدید

An Alternative PHP/DOM Implementation: eZXML

A DOM parser written in PHP, the eZXML class provides a primitive, though effective, alternative to PHP's native DOM extension. Developed as part of a larger publishing system by eZ Systems, it is freely available under the GPL from http://developer.ez.no/.

Version Control

All the examples in this section use version 1.0.3 of the eZXML class.

The eZXML class works in much the same way as PHP's native DOM library, creating a series of nested objects to represent the structure of an XML document. Each of these objects exposes standard properties, which can be used to traverse the object tree; and access specific elements, attributes, or data. In order to illustrate how this works, consider the simple XML document in Listing 8.1.

Listing 8.1 A Simple XML Document (list.xml)

<?xml version="1.0"?>
<item quantity="1">cauldron</item>
<item quantity="5">eye of newt</item>
<item quantity="1">tail of lizard</item>
<item quantity="24">bat wings</item>

The eZXML class can be used to read this XML document and convert it into a PHP structure, as Listing 8.2 demonstrates.

Listing 8.2 Creating an Object Representation of an XML Document with eZXML

// include class definition
// XML file
$xml_file = "list.xml";
// parse XML file into single string
$xml_string = join(", file($xml_file));
// create Document object
$doc = eZXML::domTree($xml_string, array("TrimWhiteSpace" => "true"));
// print structure
// uncomment the next line to view the object created
// print_r($doc);

The domTree() method of the eZXML class accepts an XML string as input and returns an object representing the XML document. This returned object is itself a collection of node objects, each one containing information about the corresponding XML node. (You can examine the structure of the returned object by uncommenting the last line of Listing 8.2.) This information, accessible as object properties, can be used to process the XML document, either to modify its structure or to format the data contained within it. For example, every node object comes with name, content, type, and children properties, which can be used to obtain information on the corresponding XML node name, value, type (whether element, attribute or text), and children respectively.

In order to demonstrate this, let's try something a little more complicated: converting an XML document into HTML. Consider Listing 8.3, which contains an invoice marked up in XML.

Listing 8.3 An XML Invoice (invoice.xml)

<?xml version="1.0"?>
<name>Joe Wannabe</name>
<line>23, Great Bridge Road</line>
<line>Bombay, MH</line>
<item cid="AS633225">
<desc>Oversize tennis racquet</desc>
<item cid="GT645">
<desc>Championship tennis balls (can)</desc>
<item cid="U73472">
<desc>Designer gym bag</desc>
<item cid="AD848383">
<desc>Custom-fitted sneakers</desc>
<delivery>Next-day air</delivery>

Listing 8.4 parses this XML document, replacing XML elements with corresponding HTML markup to create an HTML document suitable for display in a web browser. (Note that as of this writing, the eZXML class cannot handle processing instructions, and so simply ignores them.)

Listing 8.4 Converting an XML Document into HTML with eZXML

<basefont face="Arial">
<body bgcolor="white">
<font size="+3">Sammy's Sports Store</font>
<font size="-2">14, Ocean View, CA 12345, USA http://www.sammysportstore.com/</font>
// include class definition
// arrays to associate XML elements with HTML output
$startTagsArray = array(
'CUSTOMER' => '<p> <b>Customer: </b>',
'ADDRESS' => '<p> <b>Billing address: </b>',
'DATE' => '<p> <b>Invoice date: </b>',
'REFERENCE' => '<p> <b>Invoice number: </b>',
'ITEMS' => '<p> <b>Details: </b> <table width="100%" border="1" cellspacing="0"
cellpadding="3"><tr><td><b>Item description</b></td><td><b>Price</b></td>
'ITEM' => '<tr>',
'DESC' => '<td>',
'PRICE' => '<td>',
'QUANTITY' => '<td>',
'SUBTOTAL' => '<td>',
'DELIVERY' => '<p> <b>Shipping option:</b> ',
'TERMS' => '<p> <b>Terms and conditions: </b> <ul>',
'TERM' => '<li>'
$endTagsArray = array(
'LINE' => ', ',
'ITEMS' => '</table>',
'ITEM' => '</tr>',
'DESC' => '</td>',
'PRICE' => '</td>',
'QUANTITY' => '</td>',
'SUBTOTAL' => '</td>',
'TERMS' => '</ul>',
'TERM' => '</li>'
// XML file
$xml_file = "invoice.xml";
// read file into string
$xml_string = join(", file($xml_file));
// parse XML string and create object
$doc = eZXML::domTree($xml_string, array("TrimWhiteSpace" => "true"));
// start printing
// this recursive function accepts an array of nodes as argument,
// iterates through it and:
// - marks up elements with HTML
// - prints text as is
function print_tree($nodeCollection)
global $startTagsArray, $endTagsArray, $subTotals;
for ($x=0; $x<sizeof($nodeCollection); $x++)
// how to handle elements
if ($nodeCollection[$x]->type == 1)
// print HTML opening tags
echo $startTagsArray[strtoupper($nodeCollection[$x]->name)];
// recurse
// once done, print closing tags
echo $endTagsArray[strtoupper($nodeCollection[$x]->name)];
// how to handle text nodes
if ($nodeCollection[$x]->type == 3)
// print text as is
// PIs are ignored by the class

Wondering why this looks familiar? That's because it's a rewrite of an example from Chapter 3, "PHP and the Document Object Model (DOM)." That example (Listing 3.10) used PHP's native DOM library to perform the conversion from XML to HTML; this one uses the eZXML class to achieve the same result.

Very simply, the XML invoice in Listing 3.10 in order to better understand the differences between the two approaches.

Although the eZXML class does not have all the bells and whistles of PHP's native DOM extension, it can (as

/ 84