Class: XMLDoc
- Package: saf.XML
- Author: John Luxford <lux@simian.ca>
- Copyright: Copyright (C) 2001-2003, Simian Systems Inc.
- License: http://www.sitellite.org/index/license Simian Open Software License
- Version: 2.8, 2003-07-11, $Id: Doc.php,v 1.3 2007/10/06 00:06:30 lux Exp $
- Access: public
XMLDoc creates XML documents for you using DOM-like method calls.
XMLDoc is lightweight and fast, but does not deal with things like
namespaces or encoding.
New in 1.2:
- Added method query (), which allows users to traverse a set of nodes more
easily and more legibly by using the most basic subset of XPath. Currently
supports only the most basic syntax (/node1/node2/node3).
- Added the $error property.
New in 1.4:
- Added a $level parameter to the write() method, which is passed on to the
root node. -1 signifies no auto-indenting.
New in 1.6:
- Added two new methods: makeDoc() and writeToFile(). Also added a $filename
property.
New in 1.8:
- Added a $doctype property.
New in 2.0:
- Added a new parameter to the query() method that lets you return an array
of references to the resulting nodes instead of copies. This makes it
easier to use the query() method in conjunction with document updates.
New in 2.2:
- Added a makeMenu() method which uses saf.GUI.Menu to make it easier to
display a document as a hierarchy using templates.
- Updated query() to use the new saf.XML.Doc.Query class, which implements
a simple query language based on XPath. See saf.XML.Doc.Query for specifics
and examples.
- Added makeObj() and makeRef() object methods.
New in 2.4:
- Added a cache() method, which works with SloppyDOM's parseFromFile() method
to make caching of XML documents super easy.
New in 2.6:
- Added a propagateCallback() method, which compliments the new callback
functionality in saf.XML.Doc.Node. For more info, see that package in
saf/docs or DocReader.
New in 2.8:
- Added an $xquery property, and modified query() to work with the new
saf.XML.Doc.Query package.
New in 3.0:
- Added a named alias for the $root node, so that it may be referred to
by name for convenience. For example: $doc->_html. Note the
underscore, used to prevent naming conflicts.
Usage Example
<?php
$doc = new XMLDoc ();
// create a basic xhtml document
$root =& $doc->addRoot ('html');
$head =& $root->addChild ('head');
$title =& $head->addChild ('title', 'Lux\'s Home Page');
$link =& $head->addChild ('link');
$link->setAttribute ('rel', 'stylesheet');
$link->setAttribute ('type', 'text/css');
$link->setAttribute ('href', 'http://127.0.0.4/css/site.css');
$body =& $root->addChild ('body');
$anchor_top =& $body->addChild ('a');
$anchor_top->setAttribute ('name', 'top');
$h1 =& $body->addChild ('h1', 'Lux\'s Home Page');
$img =& $body->addChild ('img');
$img->setAttribute ('src', '/pix/meeting.jpg');
$img->setAttribute ('alt', 'Welcome Image');
$img->setAttribute ('border', '0');
$img->setAttribute ('style', 'float: left');
$p1 =& $body->addChild ('p', 'Lux is a guy from Windsor, Ontario. He moved to Winnipeg in 1999.');
$p2 =& $body->addChild ('p', 'This page is just Lux\'s place to put up pictures of his cats, and other things you probably don\'t care to see.');
$copyright =& $body->addChild ('p', 'Copyright (c) 2001, Lux');
$copyright->setAttribute ('align', 'center');
echo $doc->write ();
?>
Return to Top
Properties
$doc
$error
Any internal error message involved in using this class.
$client
The XML-RPC client object.
$errno
The error number (faultCode) if an error occurs.
$name
Name of the attribute.
$value
Value of the attribute.
$version
XML version. Default is 1.0
$encoding
Encoding of the XML document. Default is 'utf-8'.
Please note: Actual encoding of data is not handled by these
classes.
$doctype
May contain the entire DOCTYPE declaration tag, including the
< and >.
$root
The root node of the XML document.
$filename
If this object was read in from a file, it may be specified here.
$xquery
If query() has been called, this will contain the XMLDocQuery object.
Return to Top
Methods
XMLDoc ($version = '1.0', $encoding = 'utf-8')
Constructor method.
&addRoot ($name)
- Access: public
- Return: resource
Creates the root node object of the document.
write ($level = 0)
- Access: public
- Return: string
Generates the XML document you've created.
query ($path, $ref = 0)
- Access: public
- Return: array
Returns a set of nodes, making it easier to traverse elements
in a loop, and making the code more legible as well. Accepts a very
elementary and minimal language based on XPath (see saf.XML.Doc.Query for
specifics and examples). Returns an array of references to matching
nodes. Note: The $ref parameter is deprecated and doesn't do anything.
Results are always returned as references.
&makeDoc ($obj, $toptag)
- Access: public
- Return: object
Takes an associative array or an object and returns an XMLDoc object
created from it. $toptag is the name of the root node.
writeToFile ($file = '', $level = 0)
- Access: public
- Return: boolean
Writes the current XMLDoc object to the specified file.
makeMenu ()
- Access: public
- Return: object
Turns this document into a saf.GUI.Menu object, making it easy
to display a document as a hierarchy using templates.
makeObj ()
- Access: public
- Return: object
Calls the makeObj() method on the root node of the document tree.
&makeRefObj ()
- Access: public
- Return: object reference
Calls the makeRefObj() method on the root node of the document tree.
cache ($file)
- Access: public
- Return: boolean
Caches the current object to a file by serializing itself. Only updates
the cache file if the file modification time of the original file ($filename
property must be set or you will get an error) has changed. This method
works with SloppyDOM's parseFromFile() method to make caching XML files
super easy.
propagateCallback ($startFunction, $endFunction = false, &$obj)
Propagates a callback setting to the root node and all of its
child nodes as well. Useful for setting a "default" callback setting
which can then be overridden on a per-node basis, and for adding callbacks
to documents which were recreated from a pre-existing data source.
Return to Top