Class: SloppyDOM
- 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, 2002-10-10, $Id: Sloppy.php,v 1.5 2008/02/17 13:31:13 lux Exp $
- Access: public
SloppyDOM is a lightweight XML parser that creates an in-memory copy
of the XML document using the XMLDoc class. This allows lightweight document
parsing and modifications to take place. Note: does not maintain DOCTYPE
declarations and comments (hence Sloppy).
New in 1.2:
- Added support for CDATA blocks.
New in 1.4:
- Fixed a small bug that caused parsing to hang on multi-line comments.
New in 2.0:
- Rewrote the class to use the PHP expat extension (like it should have from
1.0), so it's much faster and more stable now. It's also backward-compatible
with 1.x.
- Also added the parseFromFile() method, and the $encoding property, which
specifies the type of encoding to send to the internal expat parser.
ISO-8859-1 is the default, and US-ASCII and UTF-8 are also supported.
- Added the following properties, which hold information pertaining to parsing
errors: $error, $err_code, $err_line, $err_byte, and $err_column.
New in 2.2:
- Fixed a spacing issue with parsing then resaving XML files, where extra
space was being added each time a file is edited.
New in 2.4:
- Changed parseFromFile() to set the XMLDoc's $filename property before returning
the object. Also improved error handling slightly in that method.
New in 2.6:
- Added an optional $cacheFile parameter to parseFromFile(), which works with
XMLDoc's cache() method to make caching an XML document effortless.
New in 2.8:
- Fixed some things that were causing allow_call_time_pass_by_reference (or
something like that) warnings.
Usage Example
<?php
$sd = new SloppyDOM ();
if ($new_doc = $sd->parse ($xml_data)) {
// use $new_doc
} else {
echo $sd->error;
}
?>
Return to Top
Properties
$doc
The XMLDoc object that was last parsed.
$error
Will contain the error message in the event of a parsing error, or false
otherwise, so that it can be used in an if (error) statement.
$client
The XML-RPC client object.
$errno
The error number (faultCode) if an error occurs.
$name
$value
Value of the attribute.
$version
XML version. Default is 1.0
$encoding
The optional encoding type to use when creating the XML parser resource.
$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.
$content
Content of the node.
$children = array ()
$attributes = array ()
Array of attribute objects.
$comment
The contents of the current comment tag.
$cdata = false
Notes whether the contents of this node should be displayed as
a <![CDATA[ ... ]]> block.
$number = 0
Contains the number of this node within its parent node. Numbers
only increase when more than one child has the same name.
$parent = array ()
A list of references to the next node in the XMLDoc hierarchy.
Destroyed before returning from parse().
$callbackStart = false
The function or method to use as a callback for the start tag
of the current XML node.
$callbackEnd = false
The function or method to use as a callback for the end tag
of the current XML node.
$callbackObject = false
The object that contains the methods listed in $callbackStart
and $callbackEnd (if they are methods and not ordinary custom or built-in
PHP functions).
$propagateCallback = false
Whether or not to propagate callback settings to new child
nodes upon their creation. Defaults to false.
$nodes
$condition
If a condition token is found, this is set to an array containing
the 'operator' and the 'left' node set of the condition.
$rules = array ()
$current
$schema
$tags
$types = array ('text', 'empty', 'data')
$parser
The parser created during calls to parse(). Destroyed before returning
from parse().
$rule = 'required'
$err_line
Will contain the error line in the event of a parsing error.
$err_code
Will contain the error code in the event of a parsing error.
$err_byte
Will contain the error byte index in the event of a parsing error.
$err_colnum
Will contain the error column number in the event of a parsing error.
$attrs = array ()
$type = false
$ns
$parents
$test
$xslt
This is an XSLT processor resource returned by the
xslt_create () function. Please note: the XSLT processor
is not created until the process method of this class is
called, so as to maintain compatibility with systems that
do not have Sablotron support installed, but are using
EasyText.
$docroot
If an RSS document is being created, this will
contain a reference to the root node.
$channel
If an RSS document is being created, this will
contain a reference to the current channel.
$comment_open = false
Return to Top
Methods
SloppyDOM ($encoding = 'ISO-8859-1')
Constructor method.
parseFromFile ($filename, $cacheFile = '')
- Access: public
- Return: object
Parses a string of XML data and returns an XMLDoc object representation of it.
Note: Returns false on failure. If you include the name of a $cacheFile,
parseFromFile() will compare its file modification time to the mod time
of the original file and use the cached copy if the original has not
been modified. This means that a document can be cached using XMLDoc's
cache() method, and then only have to be parsed once after any change
is made. Since parsing XML can be a memory intensive process, especially
on larger documents, having the luxury of not parsing the same document
upon each visitor request makes it much more appealing to use XML in web
applications. Even XML configuration files are not unreasonable (although
you want to be careful not to cache a configuration file within the
document root of your site!!!). Cache files are simply serialized XMLDoc
objects.
parse ($data)
- Access: public
- Return: object
Parses a string of XML data and returns an XMLDoc object representation of it.
Note: Returns false on failure.
tag_open ($parser, $tag, $attributes)
This is the handler for new XML tags.
cdata ($parser, $cdata)
This is the handler for new XML cdata - or content - blocks.
tag_close ($parser, $tag)
This is the handler for closing XML tags.
_default ($parser, $data)
This is the handler for all things other than opening and closing tags
and content blocks. Right now it is only used to catch CDATA tags.
Return to Top