home support FAQ resources services partners contact us contact us
 Web Application Previous  Next  
 Tutorial

264 Chapter 7 Cutting-Edge Applications

LibXML—A DOM-Based XML Parser

Since version 4.0, a new XML parser is built into PHP: LibXML. Daniel Veillard
originally created this parser for the Gnome project to offer a DOM-ready parser for
managing complex data exchange, and Uwe Steinman integrated it into PHP.
While LibXML’s internal document representation is very close to the DOM
interfaces, it’s misleading to call LibXML a DOM parser: Parsing and DOM usage
really happen at different times in a document’s life. It would be feasible to create an
API above Expat to provide a DOM interface.The LibXML library makes this much
easier, though—it’s merely a matter of changing the API to match the DOM
specification. Indeed, there is a GDome module in Gnome, which implements a
DOM interface for LibXML.

Note: At the time of this writing, the LibXML API in PHP was being finalized. It
was unstable and contained bugs—nonetheless it already showed the tremendous
benefits the finished LibXML API will offer.Therefore, we decided to document the
basic principles here and provide some examples; if changes occur, we’ll document
them on the book’s Web site.

Overview

Most developers will agree that an XML document is best represented in a tree
structure. LibXML provides a nice API to construct trees and DOM-like data
structures from an XML file.When you parse a document with LibXML, PHP
constructs a set of classes, and you’ll work with them directly. By invoking functions
on these classes, you can access all levels of the structure and modify the document.
The two most important objects you’ll spot when working with LibXML are
document and node objects.

XML Documents

The abstract XML document is represented in a document object. Such objects are
created by the functions xmldoc(), xmldocfile(), and new_xmldoc().
The function xmldoc() takes as its only argument a string containing an XML
document.The xmldocfile() function behaves very similarly, but takes a filename as
argument.To construct a new, blank XML document, you can use new_xmldoc().
All three functions return a document object, which has four associated methods
and one class variable:
n root()
n add_root()
n dtd()
n dumpmem()
n version
Previous  Next  
Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist