Appendix B: Introducing eZXML


The eZXML class is an eXtensible Markup Language (XML) Document Object Model (DOM) parser written in Hypertext Preprocessor (PHP) language. The eZXML class provides an alternative approach to DOM implementation in PHP and lets you represent XML document in the form of nested objects. You do not require external libraries to support the eZXML class, because it is compatible with the libXml library.

The eZXML class generates a DOM tree after parsing the XML document, and generates an error message on encountering an incorrect XML document. The eZXML class creates a series of objects containing information about the XML nodes. Each object contains standard properties that enable you to traverse the DOM tree and access its attributes. You can develop PHP applications using the standalone libraries of the eZXML package.

This appendix describes how to create DOM tree of an XML document and how to convert an XML document into HTML using eZXML.

Working with eZXML

eZXML parses XML documents and creates a DOM tree representation from the XML document. eZXML creates objects to represent the XML document. Each object encapsulates properties that help traverse the DOM tree and access its elements and attributes. The elements of the eZXML class are defined in the eZXML.php file.

You need to include the eZXML.php file in the PHP script to parse the XML documents. eZXML.php is a procedural file that is defined in the eZXML class. The eZXML.php file defines the following members :

  • NamespaceStack: Contains the namespaces.

  • CurrentNameSpace: Specifies the current namespace.

  • DomDocument: Specifies a reference to the DOM document.

  • & eZXML::domTree ($xmlDoc, $params = array()): Returns a DOM object tree from the XML document.

  • &eZXML::parseAttribute($attibuteString): Parses the attributes of the DOM tree and returns the value, False, if the string passed as argument does not contain any attributes.

Creating the DOM Tree of an XML Document using eZXML

The eZXML class contains the domTree() method that accepts XML strings as arguments and returns the DOM tree representation of an XML document. The object, which the domTree() method returns, contains information corresponding to the nodes of the XML document. You can use the information stored in the objects to create, modify, and format the XML document.

Listing B-1 shows how to create employee.xml file:

Listing B-1: Creating the employee.xml File
start example
 <?xml version="1.0" encoding="UTF-8"?> <EMPLOYEEINFORMATION> <EMPLOYEE> <NAME ID="E001">George</NAME> <AGE>35</AGE> <DEPARTMENT>RESEARCH AND DEVELOPMENT</DEPARTMENT> <DESIGNATION>BRANCH MANAGER</DESIGNATION> </EMPLOYEE> <EMPLOYEE> <NAME ID="E002">John</NAME> <AGE>45</AGE> <DEPARTMENT>HUMAN RESOURCE</DEPARTMENT> <DESIGNATION>MANAGER</DESIGNATION> </EMPLOYEE> </EMPLOYEEINFORMATION> 
end example
 

The above listing creates the employee.xml file that stores the employee information, such as name, ID, department, and designation. You can create the DOM object tree representation of the employee.xml file using eZXML, as shown in Listing B-2:

Listing B-2: Creating Object Representation of the employee.xml File
start example
 <?php // include class definition include_once( "./ezpublish-3.4.0/lib/ezutils/classes/ezdebug.php" ); include_once( "./ezpublish-3.4.0/lib/eZXML/classes/ezdomnode.php" ); include_once( "./ezpublish-3.4.0/lib/eZXML/classes/ezdomdocument.php" ); include_once( "./ezpublish-3.4.0/lib/eZXML/classes/eZXML.php" ); // XML file $xmlFile = "employee.xml"; // parse XML file into single string $xmlString = join("", file($xmlFile)); // create Document object $doc = eZXML::domTree($xmlString, array("TrimWhiteSpace"=> "true")); print_r($doc); ?> 
end example
 

The above listing creates a DOM tree using the eZXML class. The eZXML.php file is included in the script to parse the well- formed XML document, employee.xml. In the above listing, the object created by eZXML contains the node objects, which store information corresponding to the XML nodes.

Converting an XML Document into HTML using eZXML

eZXML converts XML documents into the HTML format. For example, you can create a file called customer.xml and convert into an HTML format.

Listing B-3 shows how to create the customer.xml file:

Listing B-3: Creating the customer.xml File
start example
 <?xml version="1.0" encoding="UTF-8"?> <Shopping> <customerInfo> <custName>John</custName> <address>1600 Pennsylvania Ave. NW, Washington, DC 20500</address> <date>2001-09-15</date> </customerInfo> <items> <item custid="3225"> <product>Air Conditioners</product> <price>16235.00</price> <quantity>1</quantity> <subtotal>262235.00</subtotal> </item> <item custid="3226"> <product>Dryers</product> <price>129.99</price> <quantity>4</quantity> <subtotal>139.96</subtotal> </item> <item custid="2345"> <product>Vacuum Cleaners</product> <price>4139.99</price> <quantity>2</quantity> <subtotal>4139.99</subtotal> </item> <item custid="4576"> <product>Washers></product> <price>349.99</price> <quantity>2</quantity> <subtotal>349.99</subtotal> </item> </items> </Shopping> 
end example
 

The above listing creates the customer.xml file that stores billing information, such as product name, billing date, quantity, and product price.

You can obtain the HTML format of the customer.xml file after running the PHP script in your Web browser.

Listing B-4 shows how to convert the customer.xml file into the HTML format:

Listing B-4: Converting customer.xml into HTML Format
start example
 <?php   // Include class definition  include("eZXML.php");  // Arrays to associate XML elements with HTML output  $startTags = array (    'CUSTOMER'=> '<p> <b>Customer: </b>',     'ADDRESS'=> '<p> <b>address: </b>',     'DATE'=> '<p> <b>date: </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>     <td><b>Quantity</b></td><td><b>Sub-total </b></    td></tr>',     'ITEM'=> '<tr>',     'PRODUCT'=> '<td>',     'PRICE'=> '<td>',     'QUANTITY'=> '<td>',     'SUBTOTAL'=> '<td>',     'TERMS'=> '<p> <b>Rules and Regulation: </b> <ul>',     'TERM'=> '<li>'  );  $endTags = array (    'LINE'=> ', ',     'ITEMS'=> '</table>',     'ITEM'=> '</tr>',     'PRODUCT'=> '</td>',     'PRICE'=> '</td>',     'QUANTITY'=> '</td>',     'SUBTOTAL'=> '</td>',     'TERMS'=> '</ul>',     'TERM'=> '</li>'  );  // XML file  $xmlfile = "customer.xml";  $xmlString = join("", file($xmlFile));  // parse XML string and create object  $doc = eZXML::domTree($xmlString, array("TrimWhiteSpace" => "true"));  // start printing  print ($doc->children);  // Accepts an array of nodes recursively as argument. // Iterates through the XML document. // Constructs HTML markups. // Displays the XML contents. function print ($NODE)  {    global $startTags, $endTags, $Totals;     for ($t=0; $t<sizeof($NODE); $t++)      {         // How to handle elements        if ($NODE[$t]->type == 1)        {          // Displays the opening tags           echo $startTags[strtoupper($NODE[$t]->name)];           // recurse           print ($NODE[$t]->children);           // Displays the closing tags           echo $endTags[strtoupper($NODE[$t]->name)];        }        // How to handle text nodes         if ($NODE[$t]->type == 3)         {          // Printing the text           echo($NODE[$t]->content);         }     }  }  ?> 
end example
 

The above listing converts the customer.xml file into the HTML format using the eZXML class. In the above listing, the eZXML.php file is included in the script to parse and manipulate the customer.xml file. The eZXML.php file contains functions, which represent XML document in a DOM tree format by creating nested objects.

In the above listing, the associative arrays, $startTags and $endTags, store the HTML markup, if the node is of element type. If the node is of text type, the content of the text node is displayed in the Web browser.




Integrating PHP and XML 2004
Integrating PHP and XML 2004
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 51

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net