Instantiating a DOMIT! Document

In DOMIT!, a DOM Document is represented by the DOMIT_Document class.

Below is an example of the cd collection XML string from the previous page being instantiated as a DOMIT_Document.

  • First, the DOMIT_Document is created and assigned to the variable $cdCollection.
  • Then, an XML string is passed to the parseXML method.


$cdCollectionString = "<cdlibrary><cd id=\"0001\">
	<name>Robbie Fulks</name>
	<title>Couples in Trouble</title></cd>
	<cd id=\"0002\"><name>Richard Thompson</name>
	<title>Mock Tudor</title></cd><cd id=\"0003\">
	<name>Keller Williams</name>
	<title>Laugh</title></cd></cdlibrary>";

$cdCollection =& new DOMIT_Document();
$success = $cdCollection->parseXML($cdCollectionString, true);

parseXML returns true if the parsing is successful.

NOTE: You instantiate a DOMIT! Lite document like this:

$cdCollection =& new DOMIT_Lite_Document().

Expat vs. SAXY

Note that the second parameter to the parseXML method should be set to true if you wish to use the SAXY parser and false if you wish to use the Expat browser. The default value is true.

However, if you specify that Expat should be used, and Expat is unavailable, DOMIT! will invoke the SAXY parser in its place.

parsedBy

You can query your document using the parsedBy method to find out which parser was used to generate the document.

$parserName = $cdCollection->parsedBy;

loadXML

You can also use the loadXML method to load an XML string from a text file.

$cdCollection =& new DOMIT_Document();
$success = $cdCollection->loadXML("/xml/cdcollection.xml");

setConnection

The setConnection method allows you to manually set the parameters of the http connection when a file is accessed remotely.

For example:

$xmldoc->setConnection('http://www.engageinteractive.com', '80');

setAuthorization

If basic HTTP authorization is required for the connection, the setAuthorization method allows you to specify a user name and password:

$xmldoc->setAuthorization('johndoe', 'xyzzy');

setProxyConnection

The setProxyConnection method allows you to manually set the parameters of a proxy http connection:

$xmldoc->setProxyConnection('http://www.myProxy.com', '8080');

setProxyAuthorization

If basic HTTP authorization is required for the proxy connection, the setProxyAuthorization method allows you to specify a user name and password:

$xmldoc->setProxyAuthorization('johndoe', 'xyzzy');

getErrorCode and getErrorString

Both the parseXML and loadXML methodsreturn false if an error is encountered while parsing. You can find out more about the error with the getErrorCode and getErrorStringmethods.

getErrorCode returns an integer designating the number of the error, while getErrorString returns a textual description of the error code:

$success = $cdCollection->loadXML("/xml/cdcollection.xml");

if (!$success) {
	echo "Error number " . $cdCollection->getErrorCode() . ": " . 
				$cdCollection->getErrorString();
}

resolveErrors

If you call the resolveErrors method just before parsing, DOMIT! will attempt to detect and repair any invalid XML.

$cdCollection =& new DOMIT_Document();
$cdCollection->resolveErrors(true);
$success = $cdCollection->loadXML("/xml/cdcollection.xml");

parseXML_utf8 and loadXML_utf8

If you are dealing with non-ASCII characters, you can use the loadXML_utf8 and parseXML_utf8 methods to encode your text in UTF-8 format.

There are several preconditions to using these methods:

  • Your original string must use ISO-8859-1 encoding. Any other encoding will likely produce corrupted text.
  • Expat must be included in your PHP distribution, since these functions use Expat's UTF8_encode function.

For these reasons, it is no longer recommended that these methods be used. Refer instead to the appendEntityTranslationTable method.

If you still want to use these methods, here is an example:

$cdCollection =& new DOMIT_Document();
$success = $cdCollection->loadXML_utf8("/xml/cdcollection.xml");

printUTF8Header

Note that before your browser can correctly display UTF-8 encoded text, you must inform it by calling the method DOMIT_Utilities::printUTF8Header(). This must be done before any output is sent to the browser.


Documentation generated by ClassyDoc, using the DOMIT! and SAXY parsers.
Please visit Engage Interactive to download free copies.